Skip to content

XMLParser

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

XMLParser is ...

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