View Full Version : How to parse this XML file ?
probine
15th February 2006, 08:58
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.
jpn
15th February 2006, 09:39
The QDom classes are typically used as follows:
QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
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.
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
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 (http://doc.trolltech.com/4.1/qdomdocument.html#details)
probine
3rd April 2006, 22:05
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 ?
Did you take a look at the QDomElement documentation (http://doc.trolltech.com/4.1/qdomelement.html#text)?
wysota
3rd April 2006, 22:46
In such a simple case it'll be faster to remove all xml tags :)
Lemming
3rd April 2006, 23:22
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.
probine
4th April 2006, 08:16
Hi wysota, I posted a very simple example. In reality the Xml file is much larger.
wysota
4th April 2006, 09:05
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.