View Full Version : XML Getting attributes of a specific value

22nd September 2009, 20:49
I have an XML document in this format:

<h1 Name="foo">
<h2 Name="goo" h2ID="0">
<h2 Name="hoo" h2ID="1">
<h2 Name="joo" h2ID="2">

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:

QString Program::getH2Name(int h2ID)
QDomElement root = domDocument.documentElement();
QDomElement child1 = root.firstChildElement("h1");
QDomElement child2 = child1.firstChildElement();
if (child2.tagName() == "h2") {
return child2.attribute("Name");

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

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