Results 1 to 6 of 6

Thread: how to modify the text of node in xml docment?

  1. #1
    Join Date
    Apr 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to modify the text of node in xml docment?

    hi,everybody.

    some xml docment like this:
    <root><firstchild>text<firstchild></root>

    i want to change the "text" to "upatetext",and i try the function "setNodeValue()",
    but it doesn't work.

    can anyone help me?
    thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Boston, MA
    Posts
    40
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to modify the text of node in xml docment?

    Qt Code:
    1. QDomDocument xmlDocument;
    2. if ( !xmlDocument.setContent(&xmlFile, true, &errorStr, &errorLine, &errorColumn) )
    3. {
    4. ....
    5. }
    6.  
    7. QDomElement rootElement = xmlDocument.documentElement();
    8. rootElement.firstChildElement("firstchild").firstChild().setNodeValue("upatetext");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to modify the text of node in xml docment?

    Just a comment -
    Actually the text is a child node. You can identify it by node.isText() function.

    Hence you need to get the firstchildelement of "firstchild" node

  4. #4
    Join Date
    Dec 2008
    Location
    Lithuania
    Posts
    10
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to modify the text of node in xml docment?

    hi,
    I have one problem.

    some xml
    <?xml version='1.0'?>
    <configs>
    <dbName>database</dbName>
    <userName>user</userName>
    </configs>

    when xml document is like this everithing work fine:
    Qt Code:
    1. QDomElement rootElement = cfgData.documentElement();
    2. rootElement.firstChildElement("dbName").firstChild().setNodeValue("newdbname");
    3. rootElement.firstChildElement("userName").firstChild().setNodeValue("newusername");
    4.  
    5. file = new QFile("config.xml");
    6. file->open(QIODevice::WriteOnly | QIODevice::Text);
    7. xml = new QTextStream(file);
    8. xml->operator <<(cfgData.toString());
    9. file->close();
    10. delete file;
    11. delete xml;
    To copy to clipboard, switch view to plain text mode 

    when user name or db name set empty (this is need when I dont want to know the last connection data) and save the xml data into file, the tags
    <dbName>database</dbName>
    <userName>user</userName>
    changed to <userName/> and <dbName/>. but when I wanna save new text into this tags with setNodeValue(). after saving xml data tags still empty. when I change the tags declaration like this <dbName>...</dbName> <userName>...</userName> eveithing work fine.

    WHERE IS MY MISTAKE???

    thanks....

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to modify the text of node in xml docment?

    <dbName>database</dbName>
    Here you have a DOM node.
    <dbName />
    Here you haven't so you must create one in qt and append it. Like
    Qt Code:
    1. QDomText t = doc.createTextNode("Hello World");
    2. dbName.appendChild(t);
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lykurg for this useful post:

    foggy-mind (26th February 2009)

  7. #6
    Join Date
    Dec 2008
    Location
    Lithuania
    Posts
    10
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Post Re: how to modify the text of node in xml docment?

    yea, your right. thanks.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.