Since your sender provides you with invalid XML, you must correct it before it gets into the parser if you want to avoid this exception. If you cannot correct the sender, you will need a preprocessing step.
If the situation is simply that after the closing tag you received extra null bytes that were not allocated by one of the answers to another answer, perhaps this can be easily achieved by transferring your input stream to the FilterInputStream , which you implement to skip null bytes
If the problem is more complicated than just null characters, you will of course need a more sophisticated filter, which can be difficult.
If you use a ContentHandler , you can add a callback to it so that it can inform the calling code when the processed end tag is processed, and based on this knowledge, the calling code can have logic in its handler to exclude it simply bypassing it if the end has been signaled. At that moment, everything the parser was supposed to do was probably done anyway! But this solution does not seem to apply to your situation.
Don roby
source share