Writing XML Nodes in QtXML (QDomElement)

人走茶凉 提交于 2020-01-05 07:50:37

问题


I would like to write Nodes like

<name>Peter</name> 

(with start and end tag) into a QDomDocument.

When I create QDomElements and append them as child to a parent element:

QDomElement node = doc.createElement("node");
parent.appendChild(node);

They are added as

<node/>

to the parent element. The parent automatically gets a start and end tag so the file would look like this:

<parent>
    <node/>
</parent>

But how do I add a value to my node so that it looks like I want it (with value between start and end tag). Adding a new QDomElement as child to node it would just look like . Adding attribute would show up like ?

Would be great if anyone could help me! Thanks!


回答1:


Create a text node using DOM Document, and add it to your newly created element as a child:

QDomElement node = doc.createElement("name");
parent.appendChild(node);
// Now, add a text element to your node
node.appendChild( doc.createTextNode( "Peter"));


来源:https://stackoverflow.com/questions/3934630/writing-xml-nodes-in-qtxml-qdomelement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!