PDA

View Full Version : Xml parsing



probine
14th December 2006, 13:05
I want to parse a very simple QString, which contains text in XML format

<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<method>loginRequest</method>
<userName>user</userName>
<userPassword>password</userPassword>
</data>

So far I am able to verify that this XML is well formed.

Now, I would like to get the information inside each tag. For example: I would like to get "user", which is inside the <userName> tag.

From the documentation I have understood that the class and funtion should return the value of whatever is inside the tag:
void * QXmlReader::property ( const QString & name, bool * ok = 0 ) const

That value is "Void *", how do I convert this to QString ?

wysota
14th December 2006, 13:10
For short xml documents it might be better to use QDomDocument approach as the memory overhead is minimal and it's much easier to operate using DOM.

If you insist on using QXmlReader approach then the "property()" method won't help you - it is for retrieving properties for the parser, not tag contents of the xml file. I'm not sure, as I never used SAX to parse an XML file, but I think you need to set the contents handler to be able to retrieve data from the file.

probine
14th December 2006, 13:25
Ok, QDomDocument is fine, the question is:

How can I verift with the QDomDocument class that the XML is well formed ?

wysota
14th December 2006, 13:37
I think QDomDocument::setContent() will return false in such situation.

Conel
15th December 2006, 12:28
Yes, and even more: it will return additional information in other parameters of this method (where it happened and why)


QDomDocument doc;
QString errorStr;
int errorLine;
int errorColumn;

if (!doc.setContent(&file, true, &errorStr, &errorLine, &errorColumn))
{
//handling error here
file.close();
return false;
}

file.close();