How to tell Java SAX Parser to ignore invalid character references?

24,413

Solution 1

Use XML 1.1! skaffman is completely right, but you can just stick <?xml version="1.1"?> on the top of your files and you'll be in good shape. If you're dealing with streams, write a wrapper that rewrites or adds that processing instruction.

Solution 2

You're going to have to clean up your XML, I'm afraid. Such characters are invalid according to the XML spec, and no amount of persuasion is going to convince the parser otherwise.

Valid XML characters for XML 1.0:

  • U+0009
  • U+000A
  • U+000D
  • U+0020U+D7FF
  • U+E000U+FFFD
  • U+10000U+10FFFF

In order to clean up, you'll have to pass the data through a more low-level processor, which treats it as a unicode character stream, removing those characters that are invalid.

Solution 3

This is invalid XML so no parser should parse it without error.

But you do encounter such hand-crafted invalid XML in real world. My solution is to manually insert CDATA markers to the data. For example,

  <data><![CDATA[ garbage with &invalid characters ]]></data>

Of course, you will get the data back as is and you have to deal with the invalid characters yourself.

Share:
24,413
Epaga
Author by

Epaga

Java developer @ i-net software by day Indie iOS developer of Mindscope and In-Flight Assistant by night Husband of 1, dad of 4

Updated on July 09, 2022

Comments

  • Epaga
    Epaga almost 2 years

    When trying to parse incorrect XML with a character reference such as &#x1, Java's SAX Parser dies a horrible death with a fatal error such as

        org.xml.sax.SAXParseException: Character reference "&#x1"
                                       is an invalid XML character.
    

    Is there any way around this? Will I have to clean up the XML file before I hand it off to the SAX Parser? If so, is there an elegant way of going about this?

  • Epaga
    Epaga about 14 years
    only problem is i'd have to be doing the decoding of the character entities myself... bummer.
  • Paul Clapham
    Paul Clapham about 14 years
    Nope. Invalid characters are still invalid inside CDATA sections.
  • ZZ Coder
    ZZ Coder about 14 years
    Please check the fact before you downvote. The character reference inside CDATA is treated as regular string. Even though "&#x1" represents a invalid reference but it's made of all valid characters. I do this all the time with several parsers so I know I am correct.
  • Epaga
    Epaga about 14 years
    wow, it works. are there downsides or compatibility issues with using xml 1.1?
  • wowest
    wowest about 14 years
    check out the "Rationale and list of changes for XML 1.1" at w3.org/TR/xml11/#sec-xml11 If I recall correctly, any valid xml 1.0 document is a valid in xml 1.1
  • wowest
    wowest over 11 years
    since this keeps coming in handy for people, a great shortcut to add this header is: docs.oracle.com/javase/1.4.2/docs/api/java/io/…