An XML parser based on expat.
The approach taken here was to compile to WASM with emscripten, and ship the WASM binary in the NPM package. This means you get a real, battle-tested XML parser, with 0 runtime dependencies.
To install:
npm install --save expat-wasm
To use:
import {XmlParser} from 'expat-wasm'
parser = new XmlParser()
parser.on('startElement', (name, attributes) => ...)
parser.parse('<foo/>')
parser.destroy()
You may enable expansion of external entity references, if you are very careful about not allowing access to unwanted files.
parser = new XmlParser({
systemEntity(base, sysId, pubId) {
// Check the new URL to ensure it is "safe", for your local definition of "safe".
return {
base: new URL(sysId, base).toString(),
data: Buffer.from('<!ENTITY foo "bar" >'),
}
},
})
The systemEntity
function MUST be synchronous, due to limitations of expat.
If you need to read from the network asynchronously, one approach might be to
call parser.stop()
, wait until you've got all of the needed data, then try
parsing again.
There are docs.
Requires nodejs 16 or higher, and works in a modern browser using WebPack. See the webpack-demo directory for simple WebPack example.
Note that expat currently only supports XML 1.0, edition 4.
See an online demo here.