Skip to content

XMLNode

do- edited this page Oct 1, 2024 · 37 revisions

XMLNode is the SAXEvent descendant connected to some parser internals.

This is not an attempt to implement the W3C specification directly. XMLNode instances may or may not be parts of more complex structures similar to DOM Document.

Application code must not construct instances of this class directly. XMLReader (and, thus, SAXEventEmitter) produces XMLNodes instead of raw SAXEvents.

Members

Name Type Description
src string XML source of the opening tag, from < to >
parent XMLNode Element node parent to this one, null for root element
level int 0 for root element, parent.level + 1 otherwise
children null or [XMLNode] Array of XMLNodes, for which this one is the parent

Computed Properties

Name Type Description
type SAXEvent.TYPES.* StartElement or EndElement. XMLReader emits both, children may be available on EndElement
name string Raw element name (maybe with a namespace prefix)
localName string local name (same as name unless a namespace is set)
namespaceURI string URI of the namespace of this node, or null
attributes null or AttributesMap Attributes of this node
namespacesMap NamespacesMap Set of namespace declarations for this node
xml string Same as src, except for EndElement: empty string for self enclosing tags, </${name}> otherwise
isSelfEnclosed Boolean true iif the src's penultimate character is /
isStartElement Boolean type === 'StartElement'
isEndElement Boolean type === 'EndElement'
isCharacters Boolean type === 'Characters'

Methods

detach

const o = n.detach ()

// <a href="#">Top</a> -> {
//  localName: 'a', 
//  namespaceURI: null, 
//  attributes: {href: '#'}, 
//  children: ["Top"]
//}

For an element or text node (but not comment, PI, DTD etc.), returns the JSON compatible javaScript value fully representing the node (e. g. allowing its proper serialization) and completely detached from any parser internals.

It's somewhat similar to XMLNode.toObject, but, by contrast, keeps the document order information and, therefore, may be used to serialize the XML back or perform other DOM related tasks.

Text and CDATA fragments are represented as plain strings.

Elements are represented as Object instances with following properties:

Name Type Description
localName string local name (same as name unless a namespace is set)
namespaceURI string URI of the namespace of this node, or null
attributes Object plain Object (not a Map) containing the node's attributes key-value pairs
children Array Array of detach results for the node's children, in source order

Both attributes and children may be empty but never null or undefined.

toString

Overrides the default toString() with the method reconctructing the node's source XML text fragment.

The value returned may differ from the original source because:

  • XML comments are ignored;
  • closing tags are generated by name (original spacing is lost, if any);
  • with the stripSpace option set on XMLParser / XMLReader, insignificant spaces are lost too.

The XML prolog is missing from the result (as it doesn't belong to any element, even the root one).

What's important to note, namespace prefixes are restored as is, but corresponding declarations are not guaranteed to be present in the output.

Global functions

Name, parameters Type Description
XMLNode.getLocalName (name) string Part of name after :, or the whole name unless it contains :.

Clone this wiki locally