If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Enaud
Posts: 61
Registered: ‎02-08-2013
My Device: z10
My Carrier: rogers
Accepted Solution

QXmlStreamReader and '&'

Hello All,

 

I'm having difficulty parsing an XML feed with an ampersand '&' between two elements:

<title>I am hungry & tired</title>

QXmlStreamReader reports the following error:

Expected '#' or '[a-zA-Z]', but got ' '.

It then spins forever because it can't continue parsing the file.

 

Has anyone worked around this problem? It certainly seems to pop up here and there on stackoverflow and the nokia developer site. No solutions yet though.

 

Cheers,

Eric

Please use plain text.
Developer
BBSJdev
Posts: 6,015
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QXmlStreamReader and '&'

[ Edited ]

That's because & should be encoded as &amp; within an xml structure.

Currently your xml is not valid and so XmlStreamReader is correctly reporting as such.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
Enaud
Posts: 61
Registered: ‎02-08-2013
My Device: z10
My Carrier: rogers

Re: QXmlStreamReader and '&'

Sure, I get that. I guess what I'm looking for is to be able to 'rescue' the parsing of the non-compliant XML feed.

 

Is there any way to fix this at run time?

 

Eric

Please use plain text.
Developer
BBSJdev
Posts: 6,015
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QXmlStreamReader and '&'

[ Edited ]

Are you in control of the feed?

 

If yes then search the string and replace with &amp; when encoding/writing it out.

 

If no then either report invalid XML or write your own XML reader.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
Enaud
Posts: 61
Registered: ‎02-08-2013
My Device: z10
My Carrier: rogers

Re: QXmlStreamReader and '&'

Gotcha. No easy solution. :smileyhappy:

 

Thanks for the help.

Please use plain text.
Developer
BBSJdev
Posts: 6,015
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QXmlStreamReader and '&'

LOL :-)

 

Doing a quick search on google people have written XML readers that can cope with non-compliance...

 

One popular example (in Java though)

http://nekohtml.sourceforge.net/

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
Enaud
Posts: 61
Registered: ‎02-08-2013
My Device: z10
My Carrier: rogers

Re: QXmlStreamReader and '&'

I could write a new XML parser but that stinks of effort. Any good programmer knows effort just means more bugs :smileyhappy:

 

I'm going to intercept the data between the time it's downloaded and parsed and replace all non-compliant characters, this seems like the easier way of going about it.

 

Eric

Please use plain text.
Developer
BBSJdev
Posts: 6,015
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QXmlStreamReader and '&'

[ Edited ]

I was going to suggest that if you had the option but even this isn't simple as you would need to look for valid and invalid replacements.

 

i.e don't replace &amp; with &amp;amp; :smileywink:

 

... and there are lots of valid encodings you would need to check; it's not just a find and replace.

 

In terms of effort there is probably not a lot in it.

 

The no effort solution is make the user do the work and just report "Invalid XML". :smileyhappy:


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 6,015
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QXmlStreamReader and '&'

Have you checked if QXmlReader or XMLDataAccess does the same?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
greenmr
Posts: 874
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: QXmlStreamReader and '&'

My RSS/Atom reader copes with this type of thing by loading the raw XML with a QNetworkRequest, copying the reply into a QByteArray, substituting all the invalid/illegal characters and codes in the QByteArray, then loading the QbyteArray into an XmlDataAccess with XmlDataAccess::loadFromBuffer().

 

Don't know if this is feasible in your situation.



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.