How to parse this XML file ?
This is my simple XML file:
<question>
<from>me</me>
<to>you</to>
<message>How to I parse this file?</message>
</question>
The intention is to get the different words out of the XML elements. The final result should be "me, you, How to I parse this file?".
Please guide me with a bit of code is possible.
Thank you.
Re: How to parse this XML file ?
Quote:
The QDom classes are typically used as follows:
Code:
QFile file("mydocument.xml");
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
// print out the element names of all elements that are direct children
// of the outermost element.
while(!n.isNull()) {
QDomElement e
= n.
toElement();
// try to convert the node to an element. if(!e.isNull()) {
cout << e.tagName() << endl; // the node really is an element.
}
n = n.nextSibling();
}
More information:
QDomDocument
Re: How to parse this XML file ?
The example given above prints the name of the elements, not the content of them.
What is the function call that prints the content of the elements. For example:
<xml>hello</xml>
I would like to get the content of the element <xml>, in this case the content should be "hello".
How ?
Re: How to parse this XML file ?
Did you take a look at the QDomElement documentation?
Re: How to parse this XML file ?
In such a simple case it'll be faster to remove all xml tags :)
Re: How to parse this XML file ?
Use QDomElement::text() instead of QDomElement::tagName() to get the contents of XML instead of getting the tagnames.
You can alternatively use QXmlContentHandler/QXmlSimpleReader classes to get the contents of the XML.
Re: How to parse this XML file ?
Hi wysota, I posted a very simple example. In reality the Xml file is much larger.
Re: How to parse this XML file ?
It doesn't matter that it is larger. It's important that you need the text in sequencial order and that you don't use attributes.