14

QDomElementのテキストを編集する必要があります-例:

内容が-のXMLファイルがあります

<root>    
    <firstchild>Edit text here</firstchild>
</root>

子要素のテキストを編集するにはどうすればよい<firstchild>ですか?

Qt4.7で提供されているQDomDocumentクラスの説明のQDomElementに関数が表示されません

Edit1-詳細を追加しています。

xmlファイルを読み取り、変更して保存する必要があります。ファイルのフォーマットは以下の通りです-

<root>    
    <firstchild>Edit text here</firstchild>
</root>

要素の値を編集する必要があります。xmlファイルを読み取るためのコードは-

QFile xmlFile(".\\iWantToEdit.xml");
xmlFile.open(QIODevice::ReadWrite);

QByteArray xmlData(xmlFile.readAll());

QDomDocument doc;
doc.setContent(xmlData);

//必要な値を読み取ります

//変更した値を書き戻しますか?

注:QDomElementをQDomNodeにキャストし、関数setNodeValue()を使用しようとしました。ただし、QDomElementには適用されません。

提案、コードサンプル、リンクは大歓迎です。

4

6 に答える 6

23

これはあなたが望むことをします(あなたが投稿したコードはそのままです):

// Get element in question
QDomElement root = doc.documentElement();
QDomElement nodeTag = root.firstChildElement("firstchild");

// create a new node with a QDomText child
QDomElement newNodeTag = doc.createElement(QString("firstchild")); 
QDomText newNodeText = doc.createTextNode(QString("New Text"));
newNodeTag.appendChild(newNodeText);

// replace existing node with new node
root.replaceChild(newNodeTag, nodeTag);

// Write changes to same file
xmlFile.resize(0);
QTextStream stream;
stream.setDevice(&xmlFile);
doc.save(stream, 4);

xmlFile.close();

...そしてあなたはすべて準備ができています。もちろん、別のファイルに書き込むこともできます。この例では、既存のファイルを切り捨てて上書きしました。

于 2011-07-25T18:29:33.250 に答える
7

ノード内のテキストを変更したい場合は、これをより適切で単純なソリューション(Lol4t0が書いたものと同様)で更新するだけです。'firstchild'ノード内のテキストは実際にはテキストノードになるため、次のようにします。

...
QDomDocument doc;
doc.setContent(xmlData);
doc.firstChildElement("firstchild").firstChild().setNodeValue(‌​"new text");

実際にテキストノードにアクセスし、値を変更できるようにする追加のfirstChild()呼び出しに注意してください。これは、新しいノードを作成してノード全体を置き換えるよりもはるかに簡単で、確実に高速で、侵襲性が低くなります。

于 2014-04-16T20:27:37.750 に答える
2

何が問題ですか。どんな値を書きたいですか?たとえば、休眠コードはこのxmlを変換します

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <node attribute="value">
        <inner_node inner="true"/>
        text
    </node>
</document>

<?xml version='1.0' encoding='UTF-8'?>
<document>
    <new_amazing_tag_name attribute="foo">
        <bar inner="true"/>new amazing text</new_amazing_tag_name>
</document>

コード:

QFile file (":/xml/document");
file.open(QIODevice::ReadOnly);
QDomDocument document;
document.setContent(&file);
QDomElement documentTag = document.documentElement();
qDebug()<<documentTag.tagName();

QDomElement nodeTag = documentTag.firstChildElement();
qDebug()<<nodeTag.tagName();
nodeTag.setTagName("new_amazing_tag_name");
nodeTag.setAttribute("attribute","foo");
nodeTag.childNodes().at(1).setNodeValue("new amazing text");

QDomElement innerNode = nodeTag.firstChildElement();
innerNode.setTagName("bar");
file.close();

QFile outFile("xmlout.xml");
outFile.open(QIODevice::WriteOnly);
QTextStream stream;
stream.setDevice(&outFile);
stream.setCodec("UTF-8");
document.save(stream,4);
outFile.close();
于 2011-07-23T20:19:42.737 に答える
2

これがあなたが必要とすることをするあなたのコードのバージョンです。spraffが言ったように、重要なのは、テキストタイプの「firstchild」ノードの子を見つけることです。これは、テキストがDOM内に存在する場所です。

   QFile xmlFile(".\\iWantToEdit.xml");
    xmlFile.open(QIODevice::ReadWrite);

    QByteArray xmlData(xmlFile.readAll());

    QDomDocument doc;
    doc.setContent(xmlData);

    // Get the "Root" element
     QDomElement docElem = doc.documentElement();

    // Find elements with tag name "firstchild"
    QDomNodeList nodes = docElem.elementsByTagName("firstchild"); 

    // Iterate through all we found
    for(int i=0; i<nodes.count(); i++)
    {
        QDomNode node = nodes.item(i);

        // Check the node is a DOM element
        if(node.nodeType() == QDomNode::ElementNode)
        {
            // Access the DOM element
            QDomElement element = node.toElement(); 

            // Iterate through it's children
            for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
            {
                // Find the child that is of DOM type text
                 QDomText t = n.toText();
                 if (!t.isNull())
                 {
                    // Print out the original text
                    qDebug() << "Old text was " << t.data();
                    // Set the new text
                    t.setData("Here is the new text");
                 }
            }
        }
    }

    // Save the modified data
    QFile newFile("iEditedIt.xml");
    newFile.open(QIODevice::WriteOnly);
    newFile.write(doc.toByteArray());
    newFile.close();
于 2011-07-25T15:54:03.973 に答える
0
Here is Solution to update xml tag using QdomDocument,It's work for linux ubuntu 18.04  
 steps is open xmlfile in readwrite mode, setContent of file in object of qdomdocument,update node value, save xmlfile and close xmlfile.

        QString filepath= QDir::homePath() + "/Book.xml";
        QFile file (filepath);
        file.open(QIODevice::ReadWrite);
        QDomDocument doc;
        doc.setContent(&file); doc.documentElement().firstChildElement("BookPrice").firstChild().setNodeValue(QString("$7777"));
         file.resize(0);
         QTextStream stream;
         stream.setDevice(&file);
         doc.save(stream, 4);
         file.close();
于 2020-07-20T14:05:09.143 に答える
-3

抽象化レベルをQDomNodeに上げます。はQDomText要素であるため、テキスト自体を取得しfirstchildて操作できます。value()setValue(x)

于 2011-07-20T12:45:02.997 に答える