Evented parser based on a WASM-compiled version of expat
npm install expat-wasmAn 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:
``js
import {XmlParser} from 'expat-wasm'
parser = new XmlParser()
parser.on('startElement', (name, attributes) => ...)
parser.parse('
parser.destroy()
`
You may enable expansion of external entity references, if you are very
careful about not allowing access to unwanted files.
`js`
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(''),
}
},
})
The systemEntity function MUST be synchronous, due to limitations of expat.parser.stop()`, wait until you've got all of the needed data, then try
If you need to read from the network asynchronously, one approach might be to
call
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.

