Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtXml: read/write values
Forum Updated to NodeBB v4.3 + New Features

QtXml: read/write values

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amigo421
    wrote on 1 Dec 2014, 17:19 last edited by
    #1

    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 value

    in 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>
    .......

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 2 Dec 2014, 00:02 last edited by
      #2

      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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • A Offline
        A Offline
        amigo421
        wrote on 2 Dec 2014, 10:05 last edited by
        #3

        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 text

        All 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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JKSH
          Moderators
          wrote on 2 Dec 2014, 12:44 last edited by
          #4

          [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");
          @

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • A Offline
            A Offline
            amigo421
            wrote on 2 Dec 2014, 12:53 last edited by
            #5

            thanks a lot!
            now it's clear

            1 Reply Last reply
            0
            • A Offline
              A Offline
              amigo421
              wrote on 2 Dec 2014, 21:14 last edited by
              #6

              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?

              1 Reply Last reply
              0

              1/6

              1 Dec 2014, 17:19

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved