Skip to content

XMLNode

do- edited this page Feb 8, 2025 · 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 XMLParser produce 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 [XMLNode] Array of XMLNodes, for which this one is the parent (unless the node is filtered out by XMLReader)

Computed Properties

Name Type Description
type SAXEvent.TYPES.*
name string For EndElement, the raw element name (maybe with a namespace prefix)
localName string For EndElement, the local name (same as name unless a namespace is set)
namespaceURI string For EndElement, the 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
innerText string The text content of all descendant nodes, concatenated
text string INTERNAL. Use innerText instead
xml string INTERNAL. Use toString () instead
isSelfEnclosed Boolean true iif the src's penultimate character is /
isStartElement Boolean type === 'StartElement'
isEndElement Boolean type === 'EndElement'
isCharacters Boolean type === 'Characters'
isLeaf Boolean true iif children is guaranteed to be void: so the node isSelfEnclosed or not an element at all

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 returning the XML string representing the node.

toString ()

When called without parameters, toString () returns the reconstructed node's source XML fragment.

The value returned may differ from the original source because:

  • XML comments are ignored;
  • consecutive Characters and CDATA nodes are merged together;
  • 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.

toString (options)

Called with arguments, this method returns the pretty printed XML. See XMLPrinter for the options reference.

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