Results 1 to 3 of 3

Thread: Replacing XML element value using QDomDocument

  1. #1
    Join Date
    Feb 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Replacing XML element value using QDomDocument

    Hi,
    Im using QDomDocument for xml file.

    My xml file has the following data,
    Qt Code:
    1. <?xml version="1.0"?>
    2. <config>
    3. <dbus>
    4. <gas_value>2</gas_value>
    5. </dbus>
    6. </config>
    To copy to clipboard, switch view to plain text mode 

    I want to replace <gas_value>2</gas_value> with <gas_value>100</gas_value>

    Im using following code to replace it
    Qt Code:
    1. QFile *file = new QFile("dbus_write.xml");
    2.  
    3. if (!file->open(QIODevice::ReadWrite | QIODevice::Text))
    4. {
    5. qDebug() << "unable to open xml file";
    6. return;
    7. }
    8.  
    9. QDomDocument doc("dbus_write");
    10. doc.setContent(file);
    11.  
    12. QDomElement root = doc.firstChildElement("config");
    13. QDomElement parent = root.firstChildElement("dbus");
    14. QDomElement gasvalue = parent.firstChildElement("gas_value");
    15. parent.firstChildElement("gas_value").firstChild().setNodeValue("100");
    16. QTextStream stream(file);
    17. gasvalue.save(stream, 4);
    18. file->close();
    To copy to clipboard, switch view to plain text mode 

    but this code doesn't replaces the tag <gas_value>2</gas_value> , instead it creates another tag <gas_value>100</gas_value>
    and produces following xml file
    Qt Code:
    1. <?xml version="1.0"?>
    2. <config>
    3. <dbus>
    4. <gas_value>2</gas_value>
    5. </dbus>
    6. </config>
    7.  
    8. <gas_value>100</gas_value>
    To copy to clipboard, switch view to plain text mode 

    I tried using replaceChild() method, but results in same output.

    Any thoughts?
    thanks.

  2. #2
    Join Date
    Feb 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re:[solved] Replacing XML element value using QDomDocument

    solved.. below code works fine for me..

    QFile *file = new QFile("dbus_write.xml");

    if (!file->open(QIODevice::ReadWrite | QIODevice::Text))
    {
    qDebug() << "unable to open xml file";
    return;
    }
    QByteArray xmlData(file->readAll());
    QDomDocument doc("dbus_write");
    doc.setContent(xmlData);

    QDomElement root = doc.firstChildElement("config");
    QDomElement parent = root.firstChildElement("dbus");
    QDomElement gasvalue = parent.firstChildElement("gas_value");
    parent.firstChildElement("gas_value").firstChild() .setNodeValue("100");
    file->resize(0);
    QTextStream stream(file);
    stream.setDevice(file);
    doc.save(stream, 4);
    file->close();

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: [solved] Replacing XML element value using QDomDocument

    You should address the memory leak surrounding the (unnecessary) use of the heap for your QFile instance.

Similar Threads

  1. Replacing a Layout
    By Gekirou in forum Qt Programming
    Replies: 9
    Last Post: 4th August 2011, 10:21
  2. Replacing QtScript with QML
    By inpoculis789 in forum Newbie
    Replies: 0
    Last Post: 19th July 2010, 14:59
  3. Split QDomDocument to new QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 10:59
  4. QDomDocument inside other QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 16:27
  5. Replacing One image with another
    By merry in forum Qt Tools
    Replies: 6
    Last Post: 8th February 2007, 14:22

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
  •  
Qt is a trademark of The Qt Company.