QtXml: read/write values
-
Hi
first steps in Qt, working with XML, porting some code from MSXML SDK
and can't understand how to work with:preparing SOAP message to send and need to set some values into template, like id and password
and add additional tags,
so first of all, don't understand why these two statememnts give me different results:
@ QString s = messageXml.elementsByTagName("usr:id").at(0).nodeValue();
s = messageXml.elementsByTagName("usr:id").at(0).toElement().text();@
(this tag exists in XML and this is non-empty string)
first one - empty string
second one - gives me real string valuein other hand I have to change current value for new credentials to send new message ,
but this statement also doesn't work for me:
@messageXml.elementsByTagName("usr:id").at(0).setNodeValue(" bla-bla-bla");@
it performs nothing - value is unchanged.
what is wrong?piece of XML is:
....
<usr:user xmlns:usr="urn:or-user">
usr:iduser1</usr:id>
usr:passwordpassword1</usr:password>
</usr:user>
....... -
Hi,
Call this:
@
#include <QDebug>...
QDomNode node = messageXml.elementsByTagName("usr:id").at(0);
qDebug() << node.nodeType();
@Check the output against:
The "NodeType documentation":http://qt-project.org/doc/qt-5/qdomnode.html#NodeType-enum, and
The "nodeValue() documentation":http://qt-project.org/doc/qt-5/qdomnode.html#nodeValue
-
this returns 1 (ElementNode)
I see in doc:
*QString QDomNode::nodeValue() const
Returns the value of the node.
The meaning of the value depends on the subclass:
Name Meaning
QDomAttr The attribute value
QDomCDATASection The content of the CDATA section
QDomComment The comment
QDomProcessingInstruction The data of the processing instruction
QDomText The textAll the other subclasses do not have a node value and will return an empty string.
*
so in my case of ElementNode, NodeValue returns empty string by design?more important for me is a setNodeValue func
currently this performs nothing
I'm calling this method and check the results by printing whole XML as text in output -
[quote author="amigo421" date="1417514720"]so in my case of ElementNode, NodeValue returns empty string by design?[/quote]Correct.
messageXml.elementsByTagName("usr:id").at(0) returns a QDomElement. A QDomElement can contain multiple children (including other nested ElementNodes), so calling nodeValue() on it doesn't make sense.
In your case, your QDomElement (ElementNode) contains one QDomText child (TextNode).
Note that QDomElement::text() in a convenience function. Its return value comes from concatenating the strings of all the ElementNode's children. The ElementNode itself does have a "value".
[quote author="amigo421" date="1417514720"]more important for me is a setNodeValue func
currently this performs nothing[/quote]That's expected. As we discussed earlier, a QDomElement (ElementNode) does not have a "value".You need to call setNodeValue() on the QDomText, not the QDomElement.
@
messageXml.elementsByTagName("usr:id").at(0).firstChild().setNodeValue(" bla-bla-bla");
@ -
thanks a lot!
now it's clear -
to keep this in one topic,
could you explain me similar question related with a basic technique in Xml please?I'm going to find list tag in a tree and add a few subitems
@
// look for a node where I will insert the items into
QDomNode listNode = messageXml.elementsByTagName("li:list").at(0);// this is a subtree which should be inserted as a child a number times in a loop
// li:listItem
// <li:value datatype="str">value1</li:value>
// </li:listItem>QDomNode listNodeItem = messageXml.createElement("li:listItem"); QDomNode listNodeValue = messageXml.createTextNode("li:value"); listNodeItem.appendChild(listNodeValue); for(QString line : valuesToAdd) { listNodeValue.setNodeValue(line); listNode.appendChild(listNodeValue); }
@
it doesn't work for me, nothing added
please explain what is wrong?