Results 1 to 4 of 4

Thread: Updating xml attribute

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Updating xml attribute

    Hi,

    Just wondering what is the best way to update an attribute in xml file, I use QXmlStreamReader class for reading my xml and now want to modify some attributes within it.

    The dbcfilename attribute shown below, this can change and I'd like to save the xml file with the new change :

    Qt Code:
    1. <!DOCTYPE configurationXML>
    2. <configuration>
    3. <dbc>
    4. <config dbcfilename="testx150hs.dbc" loadonstart="1"/>
    5. </dbc>
    6. <read>
    7. <config limits="20000" />
    8. </read>
    9. </configuration>
    To copy to clipboard, switch view to plain text mode 

    Any advice is appreciated,
    Regards,
    Steve

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Updating xml attribute

    When one wants to have an easy-to-modify document structure that can be traversed and edited in any possible way (adding/removing/modifying elements/attributes...) it is highly recommened to use QDomDocument and related-classes. The stream-oriented classes are meant for fast parsing/writing of xml, not for any kind of editing...
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating xml attribute

    Thanks,

    I am using QDomDocument to create the xml, example :

    Qt Code:
    1. QDomDocument doc( "configurationXML" );
    2. QDomElement root = doc.createElement( "configuration" );
    3. doc.appendChild( root );
    4. QDomElement dbc = doc.createElement( "dbc" );
    5. root.appendChild( dbc );
    6. QDomElement read = doc.createElement( "read" );
    7. root.appendChild( read );
    8.  
    9. // etc
    To copy to clipboard, switch view to plain text mode 

    and then using QTextStream to write the file.

    I can't seem to find methods for actually amending the xml without having to re-create the dom...I know there must be some! Maybe setNodeValue?

    Regards,
    Steve
    Last edited by steg90; 17th July 2007 at 09:23.

  4. #4
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Updating xml attribute

    In QDomDocument, you have methods to look for elements (QDomElement), and this last class has setAttribute : that's what you need

  5. The following user says thank you to guilugi for this useful post:

    steg90 (17th July 2007)

Similar Threads

  1. Replies: 5
    Last Post: 16th May 2007, 11:03
  2. Replies: 15
    Last Post: 8th May 2007, 11:46
  3. Replies: 1
    Last Post: 22nd January 2007, 09:41
  4. Replies: 2
    Last Post: 4th May 2006, 19:17
  5. Problem with QScrollArea updating from 4.0.1 to 4.1.0
    By SkripT in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2006, 22:35

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.