Show part of XML file when parsing it
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 compile each recipe as XML, each of which is a split 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 can i do this? Do you guys know a quick and clean method for doing this?
Thanks in advance for your help!
+3
2 answers
Each QDomNode has
void QDomNode::save ( QTextStream & str, int indent) const
and QTextStream has
QString * string () const
therefore, you simply use these methods and get simple, coded code and flexible code (both QDomNode and QTextStream are very powerful types).
, firstChildElement (name)/nextSiblingElement (), foreach (QDomNode node, root.childNodes()) -. Xml Qt. , .
+5