Results 1 to 2 of 2

Thread: Parsing a XML-like text stream

  1. #1
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Parsing a XML-like text stream

    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>09b0ae90050be03c452ad235a1f2915ad68439 3c</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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Parsing a XML-like text stream

    You could try
    Qt Code:
    1. for $id doc("returnedtext.xml")//RootNAME/session/session_id/string()</p>
    2. return <p>{$id}</p>
    To copy to clipboard, switch view to plain text mode 
    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,
    _

Similar Threads

  1. Display an output buffer (text stream) in QWidget
    By nomiz in forum Qt Programming
    Replies: 8
    Last Post: 30th June 2011, 09:44
  2. Xml stream
    By rk0747 in forum Qt Programming
    Replies: 3
    Last Post: 9th February 2010, 18:10
  3. Parsing Text File --> Guide Needed
    By Ali_Talib in forum Qt Programming
    Replies: 11
    Last Post: 6th January 2009, 17:19
  4. Text file parsing too slow
    By Potch in forum Newbie
    Replies: 5
    Last Post: 11th July 2008, 21:16
  5. Parsing of Text files in Qt
    By shailesh in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2006, 15:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.