Skip to content

xml

Utility functions for parsing XML text and reading XML data from external sources.

It exposes the following error codes:

Error codeDescription
UNSUPPORTED_SOURCEThe source type is not supported
READ_FAILEDReading the source failed
HTTP_ERRORAn HTTP request returned an unsuccessful response
PARSE_FAILEDXML parsing failed
INVALID_XMLThe parser reported an invalid XML document

TIP

The native DOMParser is used when available. In Node.js, @xmldom/xmldom is loaded dynamically.

parse

Signature

js
xml.parse(text, options = {})

Description

Parses an XML string and returns a DOM Document.

Parameters

NameTypeRequiredDescription
textstringyesXML text to parse
optionsobjectnoParsing options
options.domParserobjectnoDOM parser exposing a parseFromString function

Returns

TypeDescription
Promise<Document>The parsed DOM document

Throws

Throws a TypeError if text is not a string or if options does not conform to the expected schema.

Throws an error with code xml.ERROR_CODES.PARSE_FAILED if parsing fails.

When the parser returns a document containing a parsererror element, the cause has code xml.ERROR_CODES.INVALID_XML.

Examples

js
import { xml } from '@kalisio/common-core'

const document = await xml.parse('<note><to>World</to></note>')

read

Signature

js
xml.read(input, options = {})

Description

Reads an external source as text, then parses the resulting content as XML.

A source can be a local file path in Node.js, a URL provided as a string or URL instance, or a Blob/File when supported by the runtime.

Parameters

NameTypeRequiredDescription
inputstring | URL | Blob | FileyesSource containing XML data
optionsobjectnoReading and parsing options
options.encodingstringnoCharacter encoding used when reading a local file in Node.js. Defaults to 'utf-8'
options.domParserobjectnoDOM parser exposing a parseFromString function

Returns

TypeDescription
Promise<Document>The parsed DOM document

Throws

Throws an error with code xml.ERROR_CODES.UNSUPPORTED_SOURCE if the source type is not supported.

Throws an error with code xml.ERROR_CODES.READ_FAILED if the source cannot be read.

For failed HTTP responses, the cause has code xml.ERROR_CODES.HTTP_ERROR and exposes the HTTP status and statusText.

Also propagates errors thrown by xml.parse.

Examples

js
import { xml } from '@kalisio/common-core'

const document = await xml.read('./note.xml')