How to parse invalid (bad / not well-formed) XML?

That “XML” is worse than invalid – it’s not well-formed; see Well Formed vs Valid XML.

An informal assessment of the predictability of the transgressions does not help. That textual data is not XML. No conformant XML tools or libraries can help you process it.

Options, most desirable first:

  1. Have the provider fix the problem on their end. Demand well-formed XML. (Technically the phrase well-formed XML is redundant but may be useful for emphasis.)

  2. Use a tolerant markup parser to cleanup the problem ahead of parsing as XML:

  3. Process the data as text manually using a text editor or
    programmatically using character/string functions. Doing this
    programmatically can range from tricky to impossible as
    what appears to be
    predictable often is not — rule breaking is rarely bound by rules.

    • For invalid character errors, use regex to remove/replace invalid characters:

      • PHP: preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $s);
      • Ruby: string.tr("^\u{0009}\u{000a}\u{000d}\u{0020}-\u{D7FF}\u{E000‌​}-\u{FFFD}", ' ')
      • JavaScript: inputStr.replace(/[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm, '')
    • For ampersands, use regex to replace matches with &: credit: blhsin, demo

      &(?!(?:#\d+|#x[0-9a-f]+|\w+);)
      

Note that the above regular expressions won’t take comments or CDATA
sections into account.

Leave a Comment