xml2js.js / sax.js: Non-whitespace before first tag

Just in case this helps someone by coming up in google:

Node.js scenario: Parsing an utf-8 encoded XML-file with xml2js.js (which in turn relies on sax.js) results in

Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char:

'error parsing xml: Error: Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar: '

When looking into the XML directly and testing it e.g. with an XML validator everything seems fine. Turns out the culprit is the so called Byte-Order-Mark (BOM), a 3byte  "Zero width no-break space" unicode character which Windows systems automatically prepend to utf-8 files. When inspecting your file with a hex editor it shows up as hex "EFBBBF".

The following line (called before passing the data on to xml2js) fixed the issue:

var cleanedString = origString.replace("\ufeff", "");
Home API Docs Status Blog Privacy Policy Terms & Conditions Imprint & Contact