Results 1 to 10 of 10

Thread: QXmlStreamReader and reading an XML with data on multiple lines

  1. #1
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QXmlStreamReader and reading an XML with data on multiple lines

    Hi all.

    I'm trying to read an XML file with QXmlStreamReader. To be more specific, I'm reading in a kml file. Everything has been working great so far. I can read the elements and their data. Problem is that I have one element like this:

    Qt Code:
    1. <coordinates> 122.0848938459612,37.42257124044786,17
    2. 122.0849580979198,37.42211922626856,17
    3. 122.0847469573047,37.42207183952619,17
    4. 122.0845725380962,37.42209006729676,17 </coordinates>
    To copy to clipboard, switch view to plain text mode 

    When I read the element data with reader.text() I have no newline in the data, giving me a string like this:
    Qt Code:
    1. 122.0848938459612,37.42257124044786,17122.0849580979198,37.42211922626856,17122.0847469573047,37.42207183952619,17122.0845725380962,37.42209006729676,17
    To copy to clipboard, switch view to plain text mode 

    As you can see, with out any delimiter between the different lines. And without any delimiter, there is no way to parse the data (e.g. using some kind of split function).

    Why would QXmlStreamReader not insert newlines in the data? How can we parse or handle this differently?

    Edit: for your information, I tried the kml parsing using DOM, but received the same results.
    Last edited by Kwakkie; 20th February 2013 at 13:46.

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    I suppose it's a matter of neccessity.

    Afterall you get the data you want as I see. And you just need to split this string using QString::split(..) function additionaly.

  3. #3
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    Can you propose a way of splitting it when there is no delimiter between the altitude and latitude on different lines?

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    You have a piece of plain string. You may split it with "17" string for example I guess?

  5. #5
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    And what happens if the altitude is something else than 17. E.g. it can be 123.47412 or something else entirely. What happens if I encounter the 17 in a latitude or longitude e.g. 122.0849170979198 .

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    You must have messed something up in your code. When I read text from an element, I get all the newlines and whitespaces.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    Probably, though I'm not sure what I could be doing wrong. I'm using Qt 4.8.3 on Windows. Code snippet:

    Qt Code:
    1. if (m_XmlReader.isStartElement())
    2. {
    3. QString element = m_XmlReader.name().toString();
    4. m_XmlReader.readNext();
    5. if (element == "coordinates")
    6. {
    7. QString coordinateList= m_XmlReader.text().toString()
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 21st February 2013 at 09:24. Reason: changed [quote] to [code]

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    How did you open the file? And what did you do afterwards?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,589
    Thanks
    13
    Thanked 1,585 Times in 1,514 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    Does this give you a different result?
    Qt Code:
    1. if (m_XmlReader.isStartElement())
    2. {
    3. if (m_XmlReader.name() == "coordinates")
    4. {
    5. QString coordinateList= m_XmlReader.readElementText();
    6. // do stuff
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QXmlStreamReader and reading an XML with data on multiple lines

    Not really. I moved from reading and parsing the xml myself to using libkml. Makes more sense anyway. Thanks for the help though.

Similar Threads

  1. Reading and rereading a file with QXmlStreamReader
    By TheRonin in forum Qt Programming
    Replies: 14
    Last Post: 30th April 2015, 15:04
  2. QXmlStreamReader not reading text within tags
    By Ceaser88 in forum Newbie
    Replies: 2
    Last Post: 24th July 2011, 11:06
  3. Replies: 0
    Last Post: 26th May 2010, 20:01
  4. Replies: 3
    Last Post: 3rd April 2010, 21:35
  5. Reading non-ASCII lines from QTcpSocket via readLine()
    By joshtn in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2008, 00:52

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.