Results 1 to 4 of 4

Thread: QDomElement::setNodeValue not working!!

  1. #1
    Join Date
    Jun 2007
    Posts
    18
    Thanks
    2

    Default QDomElement::setNodeValue not working!!

    hello:

    I have been having problems setting the value of a QDOMElement instance. The following is my code:


    Qt Code:
    1. int main (int argc, char * argv[])
    2. {
    3. QApplication app(argc,argv);
    4.  
    5. QDomDocument d("bookml");
    6. QDomElement root = d.createElement("rootelement");
    7. d.appendChild(root);
    8. QDomElement e = d.createElement("BookList");
    9. root.appendChild(e);
    10.  
    11. QDomElement e0 = d.createElement("BookName");
    12. e.appendChild(e0);
    13. e0.setNodeValue("The Da Vinci Code");
    14. QDomElement e1 = d.createElement("BookPrice");
    15. e1.setNodeValue("45.50");
    16. e.appendChild(e1);
    17.  
    18.  
    19. QFile mFile("test.xml");
    20. if ( !mFile.open(QIODevice::WriteOnly ) )
    21. {
    22. std::cout << "Failed creating file" << std::endl;
    23. return -1;
    24. }
    25. QTextStream s(&mFile);
    26. s << d.toString();
    27. mFile.close();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    And as output in Internet Explorer I get:


    Qt Code:
    1. <!DOCTYPE bookml (View Source for full doctype...)>
    2. - <rootelement>
    3. - <BookList>
    4. <BookName />
    5. <BookPrice />
    6. </BookList>
    7. </rootelement>
    To copy to clipboard, switch view to plain text mode 

    One can notice that the tags are formed but the values of the BookName and BookPrice which I am supplying using the setNodeValue method are not being seen here.

    Any help would be appreciated.

  2. #2
    Join Date
    Feb 2006
    Location
    Denmark
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDomElement::setNodeValue not working!!

    According to the QDomNode documentation, not all sub classes uses the node value. I belive you have to create a QDomCharacterData or a QDomText node as a child to the QDomElement node, and call setNodeValue(...) on that

  3. #3
    Join Date
    Mar 2015
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QDomElement::setNodeValue not working!!

    The following code:
    QDomDocument new_doc;
    auto RootNode = new_doc.createElement("Root");
    QDomText child_node = new_doc.createTextNode("/*this will be ignored*/");
    child_node.setNodeValue("value");
    new_doc.appendChild(RootNode).appendChild(child_no de);
    QString xml_as_string = new_doc.toString();
    std::cout<<xml_as_string.toStdString();

    Will produce:
    <Root>value</Root>

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDomElement::setNodeValue not working!!

    You can also pass the text to createTextNode() instead of first passing a dummy text and then overwriting it with setNodeValue().

    Cheers,
    _

Similar Threads

  1. QDomElement text
    By emrares in forum Qt Programming
    Replies: 2
    Last Post: 21st December 2010, 08:44
  2. QGraphicsSvgItem from QDomElement
    By casperbear in forum Newbie
    Replies: 0
    Last Post: 9th September 2010, 04:25
  3. QDomElement::elementsByTagName()
    By manojmka in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 11:43
  4. QDomElement
    By sabeesh in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2007, 12:55
  5. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.