XML parsing help
-
Could anyone provide me a short example of how i can use QT to parse the following XML information which is stored inside a QString variable
@
<ErrorMessage>
<Title>title</Title>
<Message>messageeeeeeeeee</Message>
</ErrorMessage>
@When the <ErrorMessage> is reached...i would like to parse Title and Mesage text contents into 2 QString variables.
-
Take a look at [[doc:QXmlStreamReader]] and in particular the constructor that takes a QString as an argument.
-
The following is returning a blank string when i try to read the text inside the tags...code and example of text bolded that im wanting to copy
@
<ErrorMessage> <Title>title</Title> <Message>messageeeeeeeeee</Message>
</ErrorMessage>
@@Error ParseError(QXmlStreamReader& streamReader)
{
Error temp;
streamReader.readNext();
while (!(streamReader.tokenType() == QXmlStreamReader::EndElement && streamReader.name() == "ErrorMessage")) {
if (streamReader.tokenType() == QXmlStreamReader::StartElement) {if (streamReader.name() == "Title") {
temp.strTitle = streamReader.text().toString();
}
if (streamReader.name() == "Message") {
temp.strMessage = streamReader.text().toString();
}
}
streamReader.readNext();
}
return temp;
}@ -
Well, I don't see any obvious mistake in your snippet.
Why don't you try to put some qDebug() statements here and there to see the name of the nodes you are processing? It would allow to check that your QXmlStreamReader object is correctly initialised and in a good state. -
Set a breakpoint early in that procedure, and step through the code in your debugger. That will guide you to your issue. You can inspect every variable along the way.