How to concatenate QDomDocument::createElement calls?

1k Views Asked by At

I do use the QtXML Module providing a "nice" DOM-Model for Qt.

The problem i encounter is, one cannot concatenate the calls making one require to create extra QDomElement variables for appending. Is there a way around this?

QDomDocument doc;
QDomProcessingInstruction xmlVers = doc.createProcessingInstruction("xml","version=\"1.0\" encoding='utf-8'");
doc.appendChild(xmlVers);

QDomElement docTool = doc.createElement("tool");
doc.appendChild(docTool);

QDateTime t = QDateTime::currentDateTime();
QString dateString = t.toString("yyyy-MM-ddTHH:mm:ss");

// 0: Correct implementation requiring extra QDomElement dateElement
QDomElement dateElement = doc.createElement("date");
dateElement.appendChild(doc.createTextNode(dateString));
docTool.appendChild(dateElement);

// 1: Concatenating create* calls without extra variable
docTool.appendChild(doc.createElement("date1").appendChild(doc.createTextNode(dateString)));

// 2: Trying to encapsulate createElement call by brackets
docTool.appendChild((((QDomElement)doc.createElement("date2")).appendChild(doc.createTextNode(dateString))));

// 3: Trying to hit the nail by elementById (Broken per documentation?!)    

docTool.appendChild(doc.createElement("date3"));
doc.elementById("date3").appendChild(doc.createTextNode(dateString));

ui->textBrowser->append(doc.toString());

Giving really strange results:

<?xml version="1.0" encoding='utf-8'?>
<tool>
 <date>2015-01-21T10:33:56</date>2015-01-21T10:33:562015-01-21T10:33:56<date3/>
</tool>

As we see 0: is correct 1: has no date tag at all 2: same as before 3: has the date tag but no textnode content

Why can one not concatenate these calls?

2

There are 2 best solutions below

0
On BEST ANSWER

appendChild() returns the node that was added. So in:

docTool.appendChild(doc.createElement("date1").appendChild(doc.createTextNode(dateString)));

you end up trying to append the text node to both the date1 element and the docTool element. This should work:

docTool.appendChild(doc.createElement("date1")).appendChild(doc.createTextNode(dateString));
2
On

I think the key is in the following sentence (Qt docs):

The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them and the QDomDocument itself are deleted.

When you create a local object with

QDomElement dateElement = doc.createElement("date");

the dateElement element is a reference of the internal tree node. Deleting this object will delete the internal object it refer to. It happens in the following call:

docTool.appendChild(doc.createElement("date1").appendChild(doc.createTextNode(dateString)));

where temporary object created by the doc.createElement("date1") call get deleted as soon as the function called, thus the referenced internal element is also get deleted.