PDA

View Full Version : Parsing a XML-like text stream



ad5xj
25th April 2014, 20:18
I am attempting to parse a web-based reply text stream that has a XML-like structure. It seems to say it is XML 1.0 but not sure how well it complies.

The content is similar to this:

<?xml version="1.0"?>
<RootNAME version="2.0" xmlns="http://www.website.com">
<session>
<session_id>09b0ae90050be03c452ad235a1f2915ad684393c</session_id>
</session>
</RootNAME>

To test, I have captured the reply text to a file I have called returnedtext.xml.

I have attempted to use QXmlQuery in a file I called myquery.xq:

doc("returnedtext.xml")//RootNAME/session/<p>string(text(session_id))</p>

I have tried to use the xmlpatterns utility to test the query. However, the utility does not return an error nor the plaintext in the <session_id> element.

I have also looked at QDomDocument but got nowhere quickly.

XML return sources from this same site do not contain attributes - only unique node names. The QXmlQuery and QXmlPatterns methods do not seem to contain a method to return the plaintext in a node that does not have a named attribute with a value or a xmls attribute.

I am new to XML parsing so any detailed C++ code and/or QXmlQuery help would be appreciated.

Environment:
Qt 5.2.1 on Linux Mint 16
CPU DUAL CORE Athlon 64 5400+
3 Gb Ram 500Gb disk with 300Gb free

anda_skoa
26th April 2014, 10:50
You could try


for $id doc("returnedtext.xml")//RootNAME/session/session_id/string()</p>
return <p>{$id}</p>

I.e. don't mix query and output.

If you are more interested in certain values for the purpose of processing in a program, then I'd recommend having a look at QXmlStreamReader instead.
It has a couple of nice properties such as:

can parse input as it comes in, no need to wait for the full response
is a QtCore class, not extra dependency required
only needs to keep a small parsing context in memory
easy to use once you get used to it



Cheers,
_