Skip to main content

Documents · XML · loader API

XMLLoader

Parse XML into an untyped JavaScript tree, with practical options for adapting namespaces and tag names to application code.

What it returns

A document tree you can adapt at the boundary.

XMLLoader does not impose a domain schema. It gives application code the XML hierarchy, then lets options handle common JavaScript ergonomics such as camelCase-like keys and namespace prefixes.

Structure

Nested tags and values

Keys

Optionally uncapitalize tag names

Namespaces

Optionally remove prefixes

Parser

Native SAX parser

Detailed guide

XMLLoader reference

The sections below cover usage, returned data, options, and parser attribution.

The XMLLoader parses XML-encoded data.

The goal of the XMLLoader is to make it easy for JavaScript applications to access XML formatted data. It is not intended to be a tool for advanced manipulation of XML data, and options provided are focused on making the returned data easier to use in JavaScript applications.

Usage​

Load XML data into a javascript data structure and preserve the original structure

import {XMLLoader} from '@loaders.gl/documents/xml';
import {load} from '@loaders.gl/core';

const data = await load(url, XMLLoader);

Load XML data into a javascript data structure and set options that make the returned data more "JavaScript friendly":

import {XMLLoader} from '@loaders.gl/documents/xml';
import {load} from '@loaders.gl/core';

const data = await load(url, XMLLoader, {xml: {uncapitalizeKeys: true, removeNSPrefix: true}});

Data Format​

Unstructured, untyped data in the form a tree of JavaScrip objects representing the hierarchy of tags in the XML file.

Options​

Pass these as options.xml; direct parseXMLSync(text, options) accepts the same fields.

OptionDefaultBehavior
removeNSPrefixfalseRemove element/attribute prefixes and namespace declarations.
uncapitalizeKeysfalseRecursively uncapitalize output keys.
textNodeName'value'Field for text accompanying children or attributes.
arrayPaths[]Normalized element paths that always produce arrays, including roots.
attributeNamePrefix''Prefix each attribute field, e.g. '@_'.
attributesGroupNameunsetPut each element's attributes in this named object.
ignoreAttributesfalseOmit attributes.
parseTagValuetrueConvert finite numeric values and lowercase booleans in element text.
parseAttributeValuefalseApply scalar conversion to attributes independently.
trimValuestrueTrim completed text and attribute values; false preserves whitespace.
processEntitiestrueDecode entities; false validates references and preserves spelling.
htmlEntitiesfalseRecognize the built-in HTML named-entity set.
stricttrueRequire well-formed XML. HTMLLoader defaults to false.
maxDepth256Maximum element nesting, integer 1–4096.

XML preserves text across text/CDATA chunks. Repeated children become arrays. Prototype-shaped names are ordinary own data fields. Strict parsing rejects malformed documents, multiple roots, duplicate attributes, invalid XML characters, and unknown entities. Processing instructions and DOCTYPE declarations are ignored; external documents are never fetched, and custom DTD entities are not expanded. This is syntax parsing, not schema/DTD validation or namespace-URI validation.

validateXML(text) from @loaders.gl/documents/xml-parser returns true or an Error, using the same strict native parser and default nesting budget.

The old _fastXML, _sax, and _parser: 'fast-xml-parser' options are removed. Migrate the supported conversion settings to the native options above. The deprecated _parser: 'internal' and 'sax' names both select this same implementation.

Attribution​

The native object conversion layer is maintained by loaders.gl. Its SAX tokenizer derives from sax-js through sax-ts and retains the ISC license in the documents package.