Results 1 to 9 of 9

Thread: Use QXmlStreamWriter to modify existing elements

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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();

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    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().

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

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