Skip to content

XMLParser

do- edited this page Nov 5, 2022 · 17 revisions

XMLParser is a... synchronous XML parser.

It takes the source XML text in the form of a String (that must be loaded entirely) and gives out an XMLNode instance representing its root (aka document) node with all hierarchy filled in.

Elements and text/CDATA nodes are present in the output, all other (comments, prolog/processing instructions and exotic stuff like DTD) are ignored.

Usage

const fs = require ('fs')
const {XMLParser} = require ('xml-toolkit')

const xml    = fs.readFileSync ('doc.xml')
const parser = new XMLParser  ({...options})

const doc = parser.process (xml)

console.log (doc.attributes)

Options

Name Default Description
useEntities true If true, the EntityResolver is in use, otherwise &...; may occur in output
useNamespaces true If true, all element attributes are scanned for xmlns... prefixes
stripSpace true if filterElements is set, otherwise false If true, text fragments are trimmed

Methods

process (src)

...

Name, Params Type Description
src String XML to parse

Return value: the XMLNode object representing the document element.

Data transformation

Sequences of text/CDATA fragments are concatenated together to atomic Characters nodes.

If useEntities option is set on (by default), Characters fragments are transformed by EntityResolver (CDATA never are).

To drop insignificant whitespace, use the stripSpace option. When it's set to true, every aggregated text fragment is trimmed down, emptied lines are ignored completely. So, for example, <foo/>\n\n<bar/> yields no Characters at all, but for a <![CDATA[cdata]]> section spaces are left in place.

Clone this wiki locally