-
Notifications
You must be signed in to change notification settings - Fork 5
XMLNode
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.
| 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
|
| 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' |
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.
Overrides the default toString() with the method returning the XML string representing the node.
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
CharactersandCDATAnodes are merged together; - closing tags are generated by name (original spacing is lost, if any);
- with the
stripSpaceoption 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.
Called with arguments, this method returns the pretty printed XML.
All line breaks in text nodes and attribute values are substituted with numerical entities (
, 
), so each piece of content lies on a single line.
Without options.space set, the result is one line without a single break. Otherwise options.space is is used to indent elements, and other options listed right below are taken into account.
| Name | Description |
|---|---|
space |
A literal string or the number of space (0x20) characters to indent opening and closing tags |
attrSpace |
The additional indent for attributes. Similarly to space, can be set as a string or a number. When set, a line is added for each attribute. Otherwise, each opening tag belong to one line |
EOL |
os.EOL by default, this value is used for line breaks |
| Name, parameters | Type | Description |
|---|---|---|
| XMLNode.getLocalName (name) | string | Part of name after :, or the whole name unless it contains :. |