Results 1 to 2 of 2

Thread: How do I get tag names of all child elements for an XML element

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How do I get tag names of all child elements for an XML element

    Considering this XML, I'm able to get the data under some tags, but under one element, there might be a few child elements, and the number of the may change. So is there any way I can get all the child tags & data associated with it ?
    Qt Code:
    1. <metadata xmlns="http://www.scaprepo.com/SCAPRepoWebService/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.scaprepo.com/SCAPRepoWebService/schema http://www.scaprepo.com/SCAPRepoWebService/schema/metadata.xsd">
    2. <entities>
    3. <entity>
    4. <id>cpe:/a:microsoft:ie:9</id>
    5. <uri>http://www.scaprepo.com/control.jsp?command=viewXML&amp;id=cpe:/a:microsoft:ie:9</uri>
    6. <desc>Microsoft Internet Explorer 9</desc>
    7. <created-date>2011-06-27</created-date>
    8. <modified-date>2013-05-24</modified-date>
    9. <properties>
    10. <property key="product">ie</property>
    11. <property key="vendor">microsoft</property>
    12. <property key="check">oval:org.secpod.oval:def:4</property>
    13. </properties>
    14. </entity>
    15. </entities>
    16. </metadata>
    To copy to clipboard, switch view to plain text mode 

    Here under properties I need to get the all the key names & data associated with each key.
    For others, since I know the tags before-hand, I'll get the data as shown below.
    Qt Code:
    1. QDomDocument domDoc;
    2. domDoc.setContent(xmlFileData);
    3. QDomNodeList id = domDoc.elementsByTagName("id");
    4. QDomNodeList uri = domDoc.elementsByTagName("uri");
    5. QDomNodeList desc = domDoc.elementsByTagName("desc");
    6. QDomNodeList crDate = domDoc.elementsByTagName("created-date");
    7. QDomNodeList mdDate = domDoc.elementsByTagName("modified-date");
    To copy to clipboard, switch view to plain text mode 

    And with the below shown method, I'm able to get data for all the child tags with key values, but I need to know a way to find key names also.
    Qt Code:
    1. QDomNodeList properties= domDoc.elementsByTagName("properties").at(0).childNodes();
    2.  
    3. for(int i = 0; i < product.count(); i++)
    4. {
    5. qDebug() << "\nElement-" + QString::number(i) + ": " << properties.at(i).toElement().text() << endl;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Kindly help me with getting tag/key values & data under properties tag. Thank you.
    Last edited by rawfool; 6th June 2013 at 14:32.

  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: How do I get tag names of all child elements for an XML element

    Ok, at a time you will have the properties tag:
    Qt Code:
    1. QDomElement properties = /*...*/;
    2. for(QDomElement property = properties.firstChildElement("property"); !property.isNull(); property = property.nextSiblingElement("property"))
    3. {
    4. // here you can work with property
    5. }
    To copy to clipboard, switch view to plain text mode 

    Also instead of get the list of all tags (elementsByTagName) better walk throug the DOM with loops as shown above.

  3. The following user says thank you to Lykurg for this useful post:

    rawfool (7th June 2013)

Similar Threads

  1. Handle child of QProcess's child.
    By gcubar in forum Newbie
    Replies: 4
    Last Post: 30th November 2012, 02:59
  2. Replies: 9
    Last Post: 23rd April 2012, 13:53
  3. Can't change form element names after copying them
    By Computer Hater in forum Qt Tools
    Replies: 0
    Last Post: 8th July 2011, 03:24
  4. Getting next child element from XML file
    By di_zou in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2009, 07:24
  5. Replies: 1
    Last Post: 10th January 2008, 14:38

Tags for this Thread

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.