开发者

QDomDocument won't insert QDomElement

I'm doing something with XML and now I'm confused. This code works perfectly:

    QDomElement new_item = doc.createElement(name);

    new_item.setAttribute("type", value开发者_如何学C.typeName());
    new_item.setAttribute("value", value.toString());

    doc.elementsByTagName(section).at(0).appendChild(new_item);

But if I would create QDomElement myself (without calling createElement method), then it doesn't get inserted into the document. Something like this doesn't work:

    QDomElement new_item;

    new_item.setTagName(name);
    new_item.setAttribute("type", value.typeName());
    new_item.setAttribute("value", value.toString());

    doc.elementsByTagName(section).at(0).appendChild(new_item);

Can anyone explain to me why I need to use createElement method ?

Thank you :)


Basically DomElement creation needs information that QDomDocument has. From Qt 4.7 documentation

Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document, the document class also contains the factory functions needed to create these objects. The node objects created have an ownerDocument() function which associates them with the document within whose context they were created.

http://doc.qt.io/archives/qt-4.7/qdomdocument.html#details (third paragraph)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜