Results 1 to 9 of 9

Thread: Use QXmlStreamWriter to modify existing elements

  1. #1
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    5

    Post Use QXmlStreamWriter to modify existing elements

    I have a XML file as below:

    <?xml version="1.0" encoding="UTF-8"?>
    <content>
    <calibrationBoard>
    </calibrationBoard>
    <calibrationOptions>
    <camera value="0"/>
    </calibrationOptions>
    </content>

    At runtime, I need to change the value of camera from "0" to "1".

    How can I use QXmlStreamWriter to achieve this purpose? I only find out how to create new elements with QXmlStreamWriter, but I do not know how to modify existing elements with it.

  2. #2
    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: Use QXmlStreamWriter to modify existing elements

    With QXmlStreamWriter you can only write the whole document, inclusive the chanced value. But if you want to chance singe values without writing the whole document each time you have to use QDomDocument.

  3. #3
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    5

    Default Re: Use QXmlStreamWriter to modify existing elements

    I use the following code to change the value of camera from "0" to "1", but the content of the xml file is not updated at all. Do I miss something?

    --------------------------------------------------------------------------------------
    QFile file;
    file.setFileName("autocal.xml");
    file.open(QIODevice::ReadWrite | QIODevice::Text);

    QDomDocument doc("autocal");
    doc.setContent(file)

    QDomNodeList list = doc.elementsByTagName("calibrationOptions");
    QDomNode parent = list.at(0);
    QDomNode n = parent.firstChild();
    if (n.isElement() && n.nodeName()=="camera")
    {
    QDomElement e = n.toElement();
    e.setAttribute("value", "1");
    }

    file.close();

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Use QXmlStreamWriter to modify existing elements

    the file is loaded in memory and changes are done in memory... to save the changes back in file just open the same file with writeonly(to erase old contents) and then save

    void QDomNode::save ( QTextStream & str, int indent ) const

  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: Use QXmlStreamWriter to modify existing elements

    Quote Originally Posted by MrDeath View Post
    the file is loaded in memory and changes are done in memory... to save the changes back in file just open the same file with writeonly(to erase old contents) and then save

    void QDomNode::save ( QTextStream & str, int indent ) const
    That's principal right, but I guess you want to read the value somewhere else in your program. So every time open, save, read in elsewhere is not very good. Therefore only create one instance and pass a pointer of the QDomDocument to the needed classes, or use a singleton approach.

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Use QXmlStreamWriter to modify existing elements

    ya.. i was just explaning the process.. ofcourse the read n write should be once.

  7. #7
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    5

    Default Re: Use QXmlStreamWriter to modify existing elements

    Thanks. The following code does update the xml file now. However, it duplicates the content.

    QFile file;
    file.setFileName("autocal.xml");
    file.open(QIODevice::ReadWrite | QIODevice::Text);

    QDomDocument doc("autocal");
    doc.setContent(file);

    //do some reading here

    QDomNodeList list = doc.elementsByTagName("calibrationOptions");
    QDomNode parent = list.at(0);
    QDomNode n = parent.firstChild();
    if (n.isElement() && n.nodeName()=="camera")
    {
    QDomElement e = n.toElement();
    e.setAttribute("value", "1");
    }

    QTextStream stream(file);
    doc.save(stream, 4);

    file.close();

    --------------------------------------------------------------------------------------


    Do I have to open the file twice like below (first time readonly, second time writeonly)?

    QFile file;
    file.setFileName("autocal.xml");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QDomDocument doc("autocal");
    doc.setContent(file);
    file.close();

    //do some reading here

    QDomNodeList list = doc.elementsByTagName("calibrationOptions");
    QDomNode parent = list.at(0);
    QDomNode n = parent.firstChild();
    if (n.isElement() && n.nodeName()=="camera")
    {
    QDomElement e = n.toElement();
    e.setAttribute("value", "1");
    }

    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream stream(file);
    doc.save(stream, 4);
    file.close();

  8. #8
    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: Use QXmlStreamWriter to modify existing elements

    Quote Originally Posted by rhf417 View Post
    Do I have to open the file twice like below (first time readonly, second time writeonly)?
    No you can also use QIODevice::setOpenMode().

  9. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Use QXmlStreamWriter to modify existing elements

    what happened?? is it working or not?

Similar Threads

  1. Replies: 32
    Last Post: 30th March 2008, 20:00

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.