Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit Value of a QDomElement?

Tags:

c++

qt

qt4

qtxml

I need to edit the text of a QDomElement - Eg

I have an XML file with its content as -

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

How do I edit the text of the child element <firstchild>?

I don't see any functions in the QDomElement of QDomDocument classes descriptions provided in Qt 4.7

Edit1 - I am adding more details.

I need to read, modify and save an xml file. To format of the file is as below -

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

The value of element needs to be edited.I code to read the xml file is -

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

QByteArray xmlData(xmlFile.readAll());

QDomDocument doc;
doc.setContent(xmlData);

// Read necessary values

// write back modified values?

Note: I have tried to cast a QDomElement to QDomNode and use the function setNodeValue(). It however is not applicable to QDomElement.

Any suggestions, code samples, links would we greatly welcome.

like image 407
Eternal Learner Avatar asked Jul 19 '11 20:07

Eternal Learner


3 Answers

This will do what you want (the code you posted will stay as is):

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

... and you are all set. You could of course write to a different file as well. In this example I just truncated the existing file and overwrote it.

like image 189
Lucky Luke Avatar answered Nov 16 '22 02:11

Lucky Luke


Just to update this with better and simpler solution (similar like Lol4t0 wrote) when you want to change the text inside the node. The text inside the 'firstchild' node actually becomes a text node, so what you want to do is:

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

notice the extra firstChild() call which will actually access the text node and enable you to change the value. This is much simpler and surely faster and less invasive than creating new node and replacing the whole node.

like image 7
Petar Avatar answered Nov 16 '22 02:11

Petar


what is the problem. What sort of values do you want to write? For example, the fallowing code converts this xml

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

to

<?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>

Code:

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();
like image 2
Lol4t0 Avatar answered Nov 16 '22 02:11

Lol4t0