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 Error on '& ' in the XML text 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 "Error on Watch "Error on New topic
Author

Error on '& ' in the XML text

Krish Lee
Greenhorn

Joined: May 31, 2006
Posts: 2
While parsing an XML file in which one of the tag is having "&", I am getting the org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.

I understand that it can be resolved if the & is passed instead of &, but the input is directly coming from user, and we can not enforce this.

Is there any other way to resolve this issue ?

[ June 02, 2006: Message edited by: Krish Lee ]

[ June 02, 2006: Message edited by: Krish Lee ]

[ June 02, 2006: Message edited by: Krish Lee ]
[ June 02, 2006: Message edited by: Krish Lee ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Krish Lee:
but the input is directly coming from user, and we can not enforce this.
Too bad. You will have to enforce it. XML parsers are not allowed to try to repair bozo XML. Perhaps it might be better to rework your system so that you don't require people to type XML into a text editor. Or provide them with tools to check the XML for well-formedness before sending it to you.
Krish Lee
Greenhorn

Joined: May 31, 2006
Posts: 2
Thanks Paul. The system actually is not asking user to enter the complete XML, but is requesting various text fields, which are then being arranged as String XML and are being passed to the service APIs.

So we can always make the change to replace the unsupported XML characters to the appropriate supported format (http://www.w3.org/TR/REC-xml/#syntax )in the string before passing it for further processing. But I was wondering if this is the only possible solution or there is some other workaround too. Any thoughts ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Oh, it's your code that's generating the bad XML. So all you have to do is to escape the characters that need to be escaped. That's just the rules of XML, I don't know why you would consider it to be a "workaround".

I have used SAX filters connected to an identity Transformer to produce XML. That way the XML software generates the document and it does it correctly. You could see an example of that here:

http://www.cafeconleche.org/books/xmljava/chapters/ch08s05.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error on '& ' in the XML text