File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Unescaped Ampersand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Unescaped Ampersand" Watch "Unescaped Ampersand" New topic
Author

Unescaped Ampersand

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Hi,

I'm trying to parse some RSS feeds using the DocumentBuilder class.

Some of the feeds have unescaped ampersands in the elements and this is causing problems.

For example, the title element might have the value "Bob & His Dog" which gets parsed as "Bob " because of the ampersand.

I have no control over the feeds.

What is the best way to handle this?

Thanks,

Drew
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, unfortunately, RSS is supposed to be an XML dialect but in real life there is RSS-creation software that isn't as good as it should be. Or maybe people are using text editors to write RSS. Anyway, the end result is malformed XML.

You could just reject it (that would be The XML Way™). The alternative is to stop using an XML parser and use something else. I've heard about ROME for parsing RSS in Java but haven't ever used it myself.
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
How would I reject it? It's not throwing an error.

Could I just reject the one element with the & or do you mean just leave out that whole feed (not a good solution).

-Drew
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Drew Lane:
How would I reject it? It's not throwing an error.
It's not throwing an exception? Then you don't have malformed XML and therefore you don't have an unescaped ampersand.

My suspicion is that you're using a SAX parser and making the common error of assuming that the characters() method in your ContentHandler will always be passed the complete contents of a text node. That isn't the case. The parser is allowed to break text into pieces and call characters() once for each piece.
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Holy Cow!

You were right!

You rawk dude. :-)

Thanks,

-Drew
Marvin Serrano
Greenhorn

Joined: Mar 11, 2010
Posts: 5
I'm having a similar problem with my XML parsing; it just stops whenever an ampersand is hit. How can I modify the characters() method to ignore that case? Any sample code would be greatly appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Of course you can't modify the parser to call the characters() method differently than what it does. You have to modify your code if you want to collect all of the character data inside an element together. See the first question in the XmlFaq.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unescaped Ampersand