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