PDA

View Full Version : Parsing XML file



Djony
8th January 2007, 12:58
Hi!
I need to parse this xml file of this structure:

<tcdescription>
<platform type="common">
<id>wdt_0003</id>
<owner>TB</owner>
<requirement>random_requirement</requirement>
<type id="module"></type>
<style type="automatic"></style>
<module>WDT</module>
<level id="0"></level>
<effort unit="h">1</effort>
<startertimeout>100000</startertimeout>
<description>
bla bla
</description>
</platform>

<platform type="vgca" os="Windows">
<activated status="true"></activated>
<timeout>bla bla</timeout>
<trace use_automatic="yes"></trace>
<debug use_automatic="yes"></debug>

<compilation type="debug" default="yes">
<executable>bla bla</executable>
</compilation>
<compilation type="release">
<executable>bla bla</executable>
</compilation>
</platform>

<platform type="vgcb" os="Windows">
<activated status="true"></activated>
<timeout>bla bla</timeout>
<trace use_automatic="yes"></trace>
<debug use_automatic="yes"></debug>

<compilation type="debug" default="yes">
<executable>bla bla</executable>
</compilation>
<compilation type="release">
<executable>bla bla</executable>
</compilation>
</platform>
</tcdescription>
--------------------------------------------------------

I use this code


QDomNode platform = domDocument.firstChild();
platform=platform.firstChild();
while (!platform.isNull())
{
QDomElement e = platform.toElement();
cout<<e.tagName().toStdString();
platform=platform.nextSibling();
}

camel
8th January 2007, 13:06
Hmm...nice of you to show us your code...but what is your point exactly? ;-)

(Could it be that you forgot a part of your message?)

Djony
8th January 2007, 13:09
You are right, I messed up my post :( The point is that my code never enters the while loop. The condition is never fullfiled, and I don't get it why. What am I doing wrong?

camel
8th January 2007, 13:13
What am I doing wrong?

My guess would be that you should try documentElement (http://doc.trolltech.com/4.2/qdomdocument.html#documentElement) instead of firstChild on the domDocument. (And perhaps it would be easier to stick with the firstChildElement and nextSiblingElement functions, instead of working with DomNodes the whole time...)

(This is a shot from the hip...no guarantees on correctness ;-)

Djony
8th January 2007, 13:32
You are great hip shooter :) Yes, that solved it, thanx. Do you maybe know how could I extract from this XML line:

<platform type="common">
--------------------------------------

string "type ="common" ?

camel
8th January 2007, 13:43
The docs are your friend ;-)


Try attributeNode (http://doc.trolltech.com/4.2/qdomelement.html#attributeNode)

i.e.

QDomAttr attr = element.attributeNode(QLatin1String("type"));

Djony
8th January 2007, 14:51
Thanks for your replies. That did it. I have another problem. When I am browsing through my tags (id,owner,requirement,module,... look at the structure of xml file in above post) I use this line

platformDetail=platformDetail.nextSiblingElement(p latformDetail.tagName());
//platformDetail is QDomElement class

And I can't get any tags. Do you have any idea what could be problem? Similar line worked fine for platform tags...

camel
8th January 2007, 15:03
Do you have any idea what could be problem?

Once again. Check the docs (http://doc.trolltech.com/4.2/qdomnode.html#nextSiblingElement):

Returns the next sibilng element with tag name tagName if tagName is non-empty; otherwise returns any next sibling element.

What does that tell you? You try to get the next sibling element with the same name as the current element. Is that what you want?

Please take the time to read through the documentation, perhaps starting from here (http://doc.trolltech.com/4.2/qdomdocument.html#details), as the qdomdocument page is a good entry point.