Results 1 to 2 of 2

Thread: XML Getting attributes of a specific value

  1. #1
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default XML Getting attributes of a specific value

    I have an XML document in this format:

    Qt Code:
    1. <h1 Name="foo">
    2. <h2 Name="goo" h2ID="0">
    3. ...
    4. </h2>
    5. <h2 Name="hoo" h2ID="1">
    6. ...
    7. </h2>
    8. <h2 Name="joo" h2ID="2">
    9. ...
    10. </h2>
    11. </h1>
    To copy to clipboard, switch view to plain text mode 

    I want to create a function that lets the user input the h2ID value, and then return the h2 Name associated with that value. How would I go about doing that?

    So far I have:

    Qt Code:
    1. QString Program::getH2Name(int h2ID)
    2. {
    3. QDomElement root = domDocument.documentElement();
    4. QDomElement child1 = root.firstChildElement("h1");
    5. QDomElement child2 = child1.firstChildElement();
    6. if (child2.tagName() == "h2") {
    7. return child2.attribute("Name");
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    This will only return the Name of the first h2 element: "goo". How do I get a specific h2 element using the h2ID? Thanks.

  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: XML Getting attributes of a specific value

    you can use QDomElement::elementsByTagName() and iterate over the returned list, till you get your id or use XQuery (->QXmlQuery).

Similar Threads

  1. Replies: 3
    Last Post: 22nd December 2010, 14:22
  2. Attributes order in QDomElement
    By ixSci in forum Qt Programming
    Replies: 3
    Last Post: 21st August 2009, 13:52
  3. Plotting specific points with qwt
    By byb810 in forum Qwt
    Replies: 0
    Last Post: 30th July 2009, 15:42
  4. changing svg graphic attributes once rendered
    By barrygp in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2008, 02:09
  5. Profile Attributes
    By joy in forum Newbie
    Replies: 1
    Last Post: 8th September 2006, 16:11

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.