views:

40

answers:

2

Hey,

Consider the following XML file :

<cookbook>
<recipe xml:id="MushroomSoup">
    <title>Quick and Easy Mushroom Soup</title>
    <ingredient name="Fresh mushrooms"
                quantity="7"
                unit="pieces"/>
    <ingredient name="Garlic"
                quantity="1"
                unit="cloves"/>
</recipe>
<recipe xml:id="AnotherRecipe">
    <title>XXXXXXX</title>
    <ingredient name="Tomatoes"
                quantity="8"
                unit="pieces"/>
    <ingredient name="PineApples"
                quantity="2"
                unit="cloves"/>
</recipe>
</cookbook>

Let's say I want to parse this file and gather each recipe as XML, each one as a separated QString.

For example, I would like to have a QString that contains :

<recipe xml:id="MushroomSoup">
    <title>Quick and Easy Mushroom Soup</title>
    <ingredient name="Fresh mushrooms"
                quantity="7"
                unit="pieces"/>
    <ingredient name="Garlic"
                quantity="1"
                unit="cloves"/>
</recipe>

How could I do this ? Do you guys know a quick and clean method to perform this ?

Thanks in advance for your help !

A: 

Qt supports both SAX and DOM. Just write a query. Here is a tutorial.

drewk
+3  A: 

Every QDomNode has

void QDomNode::save ( QTextStream & str, int indent) const

method, and QTextStream has

QString * string () const

so, you just use these methods, and obtain simple, codepage-awared and flexible code (both QDomNode and QTextStream are very powerful types).

You can iterate through elements that have particular name by using firstChildElement(name)/nextSiblingElement(name) or you can get the root element of the recipes and write foreach(QDomNode node, root.childNodes()), or smth. There are plenty of ways to precess the Xml in Qt. Look at the tutorials Trolls provided.

Max
Hey, thanks a lot for your answer ! I'll give it a try tomorrow ! I'll keep you updated with my progress !
Andy M
ok:) And you'd better check out the samples too, it is rather simple and clean.
Max
Hey, so I repeat, thanks a lot mate, it works perfectly well ! It's exactely what I needed...
Andy M