Object built-in object

ObjectXmlDocument

XmlDocument is xmlAn object of the module, which represents the entire XML document and provides access to the entire document.

XmlDocument is the root of a document tree and contains all nodes in the entire XML document. The XmlDocument object also provides the following functions:

  1. Create element nodes, text nodes, comments, processing instructions, etc.
  2. Access and modify document properties and related information (such as DTD comments and document declarations)
  3. Parse XML documents

The following is sample code that uses the XmlDocument object to parse an XML document:

1 2 3 4 5 6 7 8 9
var xml = require('xml'); var fs = require('fs'); var xmlStr = fs.readFile('test.xml'); var xmlDoc = xml.parse(xmlStr); // get document root node name var rootName = xmlDoc.documentElement.nodeName; console.log(`文档根节点名称是 ${rootName}`);

In the above code, we first use fsthe module readFile()method to read an XML file and assign the file stream to the variable xmlStr. Then we use [xml](../../module/ifs/xml.md)the module's method to parse the XML file and assign the parse()parsed object to a variable . Finally, we use the attribute of to get the document root node, get its node name, and output it to the console.XmlDocumentxmlDocxmlDocdocumentElement

Since XmlDocument is the entrance to the entire XML document, we can obtain and modify document-related information through it. For example, we can obtain and modify the XML version and standalone attributes of the document through xmlDoc.xmlVersionand respectively. xmlDoc.xmlStandaloneWe can also xmlDoc.createProcessingInstruction()create new processing instruction nodes through the method.

The XmlDocument object is a very powerful type that provides great convenience for us to process and parse XML files.

inheritance relationship

Constructor

XmlDocument

Construct an XmlDocument object

1
new XmlDocument(String type = "text/xml");

Call parameters:

  • type: String, specifies the type of document object, the default is "text/xml", if you need to process html, you need to specify "text/html"

member properties

inputEncoding

String, returns the encoding used for the document (when parsing)

1
readonly String XmlDocument.inputEncoding;

xmlStandalone

Boolean, sets or returns whether the document is standalone

1
Boolean XmlDocument.xmlStandalone;

xmlVersion

String, sets or returns the XML version of the document

1
String XmlDocument.xmlVersion;

doctype

XmlDocumentType, returns the Document Type Declaration related to the document

1
readonly XmlDocumentType XmlDocument.doctype;

For XML documents without a DTD, null is returned. This property providesXmlDocumentTypeDirect access to the object (a child node of XmlDocument).


documentElement

XmlElement, returns the root node of the document

1
readonly XmlElement XmlDocument.documentElement;

XmlElement, returns the head node of the HTML document, only valid in html mode

1
readonly XmlElement XmlDocument.head;

title

String, returns the content of the title node of the HTML document, only valid in html mode

1
readonly String XmlDocument.title;

body

XmlElement, returns the body node of the HTML document, only valid in html mode

1
readonly XmlElement XmlDocument.body;

nodeType

Integer, returns the node type of the node

1
readonly Integer XmlDocument.nodeType;

The nodeType of different objects will return different values:


nodeName

String, returns the name of the node, according to its type

1
readonly String XmlDocument.nodeName;

The nodeName of different objects will return different values:


nodeValue

String, returns the name of the node, according to its type

1
String XmlDocument.nodeValue;

The nodeName of different objects will return different values:


ownerDocument

XmlDocument, returns the root element of the node (XmlDocument object)

1
readonly XmlDocument XmlDocument.ownerDocument;

parentNode

XmlNode, can return the parent node of a node

1
readonly XmlNode XmlDocument.parentNode;

childNodes

XmlNodeList, returns the node list of the child nodes of the specified node

1
readonly XmlNodeList XmlDocument.childNodes;

children

XmlNodeList, returns the node list of the child element nodes of the specified node.

1
readonly XmlNodeList XmlDocument.children;

firstChild

XmlNode, returns the first child node of the node

1
readonly XmlNode XmlDocument.firstChild;

lastChild

XmlNode, returns the last child node of the node

1
readonly XmlNode XmlDocument.lastChild;

previousSibling

XmlNode, returns the node immediately preceding a node (at the same tree level). If there is no such node, then this property returns null.

1
readonly XmlNode XmlDocument.previousSibling;

nextSibling

XmlNode, returns the node immediately following an element (in the same tree level). If there is no such node, the attribute returns null.

1
readonly XmlNode XmlDocument.nextSibling;

firstElementChild

XmlNode, returns the first child element node of the node

1
readonly XmlNode XmlDocument.firstElementChild;

lastElementChild

XmlNode, returns the last child element node of the node

1
readonly XmlNode XmlDocument.lastElementChild;

previousElementSibling

XmlNode, returns the element node immediately preceding a node (at the same tree level). If there is no such node, then this property returns null.

1
readonly XmlNode XmlDocument.previousElementSibling;

nextElementSibling

XmlNode, returns the element node immediately following an element (in the same tree level). If there is no such node, the attribute returns null.

1
readonly XmlNode XmlDocument.nextElementSibling;

textContent

String, queries and sets the text of the selected element. When querying, returns the values ​​of all text nodes within the element node; when setting, deletes all child nodes and replaces them with a single text node.

1
String XmlDocument.textContent;

member function

load

The document is composed by parsing an XML/HTML string. Multilingualism is not supported.

1
XmlDocument.load(String source);

Call parameters:

  • source: String, the XML/HTML text to be parsed, depending on the type when the document was created

The document is composed by parsing a binary XML/HTML string and automatically converted according to the language.

1
XmlDocument.load(Buffer source);

Call parameters:

  • source:Buffer, the XML/HTML text to be parsed, depending on the type of document when it was created

getElementsByTagName

Returns a node list with all elements with the specified name

1
XmlNodeList XmlDocument.getElementsByTagName(String tagName);

Call parameters:

  • tagName: String, the tag name to be retrieved. The value "*" matches all tags

Return results:

  • XmlNodeList, with the specified tag in the document treeXmlElementNodalXmlNodeListgather. The order of returned element nodes is the order in which they appear in the source document.

This method will return aXmlNodeListObject (can be processed as a read-only array) that stores all tags in the document with the specified tag nameXmlElementNodes are stored in the order they appear in the source document.XmlNodeListThe object is "live", that is, if an element with the specified tag name is added or deleted in the document, its content will automatically be updated as necessary.


getElementsByTagNameNS

Returns a node list of all elements with the specified namespace and name

1 2
XmlNodeList XmlDocument.getElementsByTagNameNS(String namespaceURI, String localName);

Call parameters:

  • namespaceURI: String, specifies the namespace URI to be retrieved. The value "*" matches all tags
  • localName: String, the tag name to be retrieved. The value "*" matches all tags

Return results:

  • XmlNodeList, with the specified tag in the document treeXmlElementNodalXmlNodeListgather. The order of returned element nodes is the order in which they appear in the source document.

This method is similar to the getElementsByTagName() method except that it retrieves elements based on namespace and name.


getElementById

Returns the element with the specified id attribute

1
XmlElement XmlDocument.getElementById(String id);

Call parameters:

  • id: String, the id to be retrieved

Return results:

This method will traverse the descendant nodes of the document and return aXmlElementA node object that represents the first document element with the specified id attribute. .


getElementsByClassName

Returns a node list of all elements with the specified class name

1
XmlNodeList XmlDocument.getElementsByClassName(String className);

Call parameters:

  • className: String, the class name to be retrieved

Return results:

  • XmlNodeList, with the specified class name in the document treeXmlElementNodalXmlNodeListgather. The order of returned element nodes is the order in which they appear in the source document.

This method will return aXmlNodeListObject (can be processed as a read-only array), which stores all files with the specified class name in the documentXmlElementNodes are stored in the order they appear in the source document.XmlNodeListThe object is "live", that is, if an element with the specified tag name is added or deleted in the document, its content will automatically be updated as necessary.


createElement

Create element node

1
XmlElement XmlDocument.createElement(String tagName);

Call parameters:

  • tagName: String, specifies the specified name of the element node

Return results:


createElementNS

Creates an element node with the specified namespace

1 2
XmlElement XmlDocument.createElementNS(String namespaceURI, String qualifiedName);

Call parameters:

  • namespaceURI: String, specifies the element node namespace URI
  • qualifiedName: String, specifies the specified name of the element node

Return results:


createTextNode

Create text node

1
XmlText XmlDocument.createTextNode(String data);

Call parameters:

  • data: String, specifies the text of this node

Return results:

  • XmlText, returns the newly createdXmlTextNode, representing the specified data string

createComment

Create annotation node

1
XmlComment XmlDocument.createComment(String data);

Call parameters:

  • data: String, specifies the comment text of this node

Return results:


createCDATASection

createXmlCDATASectionnode

1
XmlCDATASection XmlDocument.createCDATASection(String data);

Call parameters:

  • data: String, specifies that this node specifies CDATA data

Return results:


createProcessingInstruction

createXmlProcessingInstructionnode

1 2
XmlProcessingInstruction XmlDocument.createProcessingInstruction(String target, String data);

Call parameters:

  • target: String, specifies the target of the processing instruction
  • data: String, specifies the content text of the processing instruction

Return results:


hasChildNodes

Query whether there are child nodes

1
Boolean XmlDocument.hasChildNodes();

Return results:

  • Boolean, returns true if there are any child nodes, otherwise returns false

normalize

Merge adjacent Text nodes and delete empty Text nodes

1
XmlDocument.normalize();

This method will traverse all descendant nodes of the current node and normalize the document by deleting empty Text nodes and merging all adjacent Text nodes. This method is useful for simplifying the structure of the document tree after inserting or deleting nodes.


cloneNode

Creates an exact copy of the specified node

1
XmlNode XmlDocument.cloneNode(Boolean deep = true);

Call parameters:

  • deep: Boolean, whether to deep copy, when true, the cloned node will clone all child nodes of the original node

Return results:

This method will copy and return a copy of the node on which it was called. If the argument passed to it is true, it will also recursively copy all descendant nodes of the current node. Otherwise, it only copies the current node. The returned node does not belong to the document tree and its parentNode property is null. When an Element node is copied, all its properties will be copied.


lookupPrefix

Returns the prefix matching the specified namespace URI on the current node

1
String XmlDocument.lookupPrefix(String namespaceURI);

Call parameters:

  • namespaceURI: String, specifies the matching namespace URI

Return results:

  • String, returns the matching prefix, returns null if not matched.

lookupNamespaceURI

Returns the namespace URI matching the specified prefix on the current node

1
String XmlDocument.lookupNamespaceURI(String prefix);

Call parameters:

  • prefix: String, specifies the matching prefix

Return results:

  • String, returns the matching namespace URI, returns null if not matched.

insertBefore

Insert a new child node before an existing child node

1 2
XmlNode XmlDocument.insertBefore(XmlNode newChild, XmlNode refChild);

Call parameters:

  • newChild:XmlNode, insert new node
  • refChild:XmlNode, insert a new node before this node

Return results:

  • XmlNode, returns the new child node

If newChild already exists in the document tree, it will be removed from the document tree and reinserted in its new position. Nodes from one document (or nodes created by one document) cannot be inserted into another document. That is, the ownerDocument attribute of newChild must be the same as the ownerDocument attribute of the current node.


insertAfter

Insert a new child node after an existing child node

1 2
XmlNode XmlDocument.insertAfter(XmlNode newChild, XmlNode refChild);

Call parameters:

  • newChild:XmlNode, insert new node
  • refChild:XmlNode, insert a new node after this node

Return results:

  • XmlNode, returns the new child node

If newChild already exists in the document tree, it will be removed from the document tree and reinserted in its new position. Nodes from one document (or nodes created by one document) cannot be inserted into another document. That is, the ownerDocument attribute of newChild must be the same as the ownerDocument attribute of the current node.


appendChild

Adds a new child node to the end of a node's child node list

1
XmlNode XmlDocument.appendChild(XmlNode newChild);

Call parameters:

  • newChild:XmlNode, specify the added node

Return results:

  • XmlNode, returns this new child node

If newChild already exists in the document tree, it will be removed from the document tree and reinserted in its new position. Nodes from one document (or nodes created by one document) cannot be inserted into another document. That is, the ownerDocument attribute of newChild must be the same as the ownerDocument attribute of the current node.


replaceChild

Replace a child node with another

1 2
XmlNode XmlDocument.replaceChild(XmlNode newChild, XmlNode oldChild);

Call parameters:

  • newChild:XmlNode, specify the new node
  • oldChild:XmlNode, specifies the node to be replaced

Return results:

  • XmlNode, If the replacement is successful, this method can return the replaced node. If the replacement fails, it returns null.

If newChild already exists in the document tree, it will be removed from the document tree and reinserted in its new position. Nodes from one document (or nodes created by one document) cannot be inserted into another document. That is, the ownerDocument attribute of newChild must be the same as the ownerDocument attribute of the current node.


removeChild

Remove a node from the list of child nodes

1
XmlNode XmlDocument.removeChild(XmlNode oldChild);

Call parameters:

  • oldChild:XmlNode, specifies the node to be deleted

Return results:

  • XmlNode, If the deletion is successful, this method can return the deleted node. If it fails, it returns null.

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String XmlDocument.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value XmlDocument.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable