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

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


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.


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.


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.