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:
Code:
<coordinates> 122.0848938459612,37.42257124044786,17
122.0849580979198,37.42211922626856,17
122.0847469573047,37.42207183952619,17
122.0845725380962,37.42209006729676,17 </coordinates>
When I read the element data with reader.text() I have no newline in the data, giving me a string like this:
Code:
122.0848938459612,37.42257124044786,17122.0849580979198,37.42211922626856,17122.0847469573047,37.42207183952619,17122.0845725380962,37.42209006729676,17
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.
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.
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?
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?
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 .
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.
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:
Code:
if (m_XmlReader.isStartElement())
{
QString element
= m_XmlReader.
name().
toString();
m_XmlReader.readNext();
if (element == "coordinates")
{
QString coordinateList
= m_XmlReader.
text().
toString() }
}
Re: QXmlStreamReader and reading an XML with data on multiple lines
How did you open the file? And what did you do afterwards?
Re: QXmlStreamReader and reading an XML with data on multiple lines
Does this give you a different result?
Code:
if (m_XmlReader.isStartElement())
{
if (m_XmlReader.name() == "coordinates")
{
QString coordinateList
= m_XmlReader.
readElementText();
// do stuff
}
}
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.