PDA

View Full Version : QXmlStreamReader and reading an XML with data on multiple lines



Kwakkie
20th February 2013, 11:33
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:


<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:

122.0848938459612,37.42257124044786,17122.08495809 79198,37.42211922626856,17122.0847469573047,37.422 07183952619,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.

zgulser
20th February 2013, 15:59
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.

Kwakkie
20th February 2013, 16:01
Can you propose a way of splitting it when there is no delimiter between the altitude and latitude on different lines?

zgulser
20th February 2013, 16:09
You have a piece of plain string. You may split it with "17" string for example I guess?

Kwakkie
20th February 2013, 16:11
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 .

wysota
20th February 2013, 17:11
You must have messed something up in your code. When I read text from an element, I get all the newlines and whitespaces.

Kwakkie
21st February 2013, 08:21
Probably, though I'm not sure what I could be doing wrong. I'm using Qt 4.8.3 on Windows. Code snippet:


if (m_XmlReader.isStartElement())
{
QString element = m_XmlReader.name().toString();
m_XmlReader.readNext();
if (element == "coordinates")
{
QString coordinateList= m_XmlReader.text().toString()
}
}

wysota
21st February 2013, 09:29
How did you open the file? And what did you do afterwards?

ChrisW67
21st February 2013, 23:21
Does this give you a different result?


if (m_XmlReader.isStartElement())
{
if (m_XmlReader.name() == "coordinates")
{
QString coordinateList= m_XmlReader.readElementText();
// do stuff
}
}

Kwakkie
22nd February 2013, 12:27
Not really. I moved from reading and parsing the xml myself to using libkml. Makes more sense anyway. Thanks for the help though.