2011-07-19 29 views
11

私はQDomElementのテキストを編集する必要があります -QDomElementの値を編集しますか?

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

どのように私は子要素<firstchild>のテキストを編集する - 例:

は私がように、そのコンテンツを持つXMLファイルを持っていますか?

私はQtの4.7

EDIT1で提供QDomElement QDomDocumentのクラスの説明に任意の機能が表示されない - 私はより多くの詳細を追加しています。

xmlファイルを読み込み、変更して保存する必要があります。ファイルの形式を、以下のようである -

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

要素の値は、XMLファイルを読み取るためにedited.Iコードである必要がある -

必要な値を読む
QFile xmlFile(".\\iWantToEdit.xml"); 
xmlFile.open(QIODevice::ReadWrite); 

QByteArray xmlData(xmlFile.readAll()); 

QDomDocument doc; 
doc.setContent(xmlData); 

//

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

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

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

答えて

18

これは(あるとして、あなたが投稿したコードが滞在します)あなたがやりたいことになります。

// 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(); 

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

+0

これはわずかな変更で動作します。ありがとう。 –

+1

@Eternal Learner:恩恵を忘れないでください!! – Goz

+3

実際には、内部のテキストはテキストノードなので、次のようにしてください:doc.documentElement()。firstChildElement( "firstchild")。firstChild()。setNodeValue( "new text"); //追加のfirstChild()クエリに気付く – Petar

-2

抽象レベルをQDomNodeに上げてください。 firstchildはQDomText要素なので、value()setValue(x)をテキスト自体で使用することができます。

+0

QDomNode :: firstChildはQDomNodeを返し、QDomTextは返しません。 –

+0

問題のQDomNodeは* QDomTextです。継承ツリーを確認します。実行時にノードタイプをチェックして[変換する](http://doc.qt.nokia.com/latest/qdomnode.html#toText) – spraff

+0

いいえ、 'firstchild'は' QDomElement'です。そしてそれは子供 'QDomText'を持っています。私はそれをチェックした。 – Lol4t0

1

何が問題ですか。どのような種類の価値を書いていますか? 例えば、休閑コードは、この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(); 
+0

setNodevalue関数は、特定の要素タイプに対してのみ機能します。 Qtドキュメントの 'QString QDomNode :: nodeValue()const'メソッドを見てください。それらは、setNodeValueが適用可能なノードのリストを提供します。 'QDomelement'の値を設定する必要があります。 –

+0

あなたは間違っています。私の例で分かるように、 'QDomElement' _は直接テキストを含んでいません。テキストを含む 'QDomText'要素が含まれています。 'QDomElement'にテキストが直接含まれている場合、それを置き換えるときに、要素のすべての子ノードが消去されます。 – Lol4t0

1

をここで何が必要ありませんあなたのコードのバージョンがあります。 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(); 
+0

これは、私が "QDomText t = n.toText();"代わりに "QDomText t = n.firstChild()。toText();"と読みます。 – myk

3

これをノード内のテキストを変更するときに、より簡単でより簡単な解決法(Lol4t0と同様)で更新するだけです。「のfirstChild」ノード内のテキストは、実際にテキストノードとなり、あなたが何をしたいです:

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

予告実際にテキストノードにアクセスして値を変更することができます余分のfirstChild()の呼び出し。これは、新しいノードを作成し、ノード全体を置き換えるよりはるかに簡単で確実に高速で低侵襲です。

関連する問題