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:
- Create element nodes, text nodes, comments, processing instructions, etc.
- Access and modify document properties and related information (such as DTD comments and document declarations)
- 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
9var 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 fs
the 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.XmlDocument
xmlDoc
xmlDoc
documentElement
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.xmlVersion
and respectively. xmlDoc.xmlStandalone
We 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
1new 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)
1readonly String XmlDocument.inputEncoding;
xmlStandalone
Boolean, sets or returns whether the document is standalone
1Boolean XmlDocument.xmlStandalone;
xmlVersion
String, sets or returns the XML version of the document
1String XmlDocument.xmlVersion;
doctype
XmlDocumentType, returns the Document Type Declaration related to the document
1readonly 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
1readonly XmlElement XmlDocument.documentElement;
head
XmlElement, returns the head node of the HTML document, only valid in html mode
1readonly XmlElement XmlDocument.head;
title
String, returns the content of the title node of the HTML document, only valid in html mode
1readonly String XmlDocument.title;
body
XmlElement, returns the body node of the HTML document, only valid in html mode
1readonly XmlElement XmlDocument.body;
nodeType
Integer, returns the node type of the node
1readonly Integer XmlDocument.nodeType;
The nodeType of different objects will return different values:
- XmlElement: ELEMENT_NODE(1)
- XmlAttr:ATTRIBUTE_NODE(2)
- XmlText: TEXT_NODE(3)
- XmlCDATASection: CDATA_SECTION_NODE(4)
- XmlProcessingInstruction: PROCESSING_INSTRUCTION_NODE(7)
- XmlComment:COMMENT_NODE(8)
- XmlDocument: DOCUMENT_NODE(9)
- XmlDocumentType: DOCUMENT_TYPE_NODE(10)
nodeName
String, returns the name of the node, according to its type
1readonly String XmlDocument.nodeName;
The nodeName of different objects will return different values:
- XmlElement:element name
- XmlAttr: attribute name
- XmlText: #text
- XmlCDATASection: #cdata-section
- XmlProcessingInstruction: Returns the specified target target
- XmlComment: #comment
- XmlDocument: #document
- XmlDocumentType: doctype name
nodeValue
String, returns the name of the node, according to its type
1String XmlDocument.nodeValue;
The nodeName of different objects will return different values:
- XmlElement: null
- XmlAttr: attribute value
- XmlText: the content of the node
- XmlCDATASection: the content of the node
- XmlProcessingInstruction: Returns the specified content data
- XmlComment: annotation text
- XmlDocument: null
- XmlDocumentType: null
ownerDocument
XmlDocument, returns the root element of the node (XmlDocument object)
1readonly XmlDocument XmlDocument.ownerDocument;
parentNode
XmlNode, can return the parent node of a node
1readonly XmlNode XmlDocument.parentNode;
childNodes
XmlNodeList, returns the node list of the child nodes of the specified node
1readonly XmlNodeList XmlDocument.childNodes;
children
XmlNodeList, returns the node list of the child element nodes of the specified node.
1readonly XmlNodeList XmlDocument.children;
firstChild
XmlNode, returns the first child node of the node
1readonly XmlNode XmlDocument.firstChild;
lastChild
XmlNode, returns the last child node of the node
1readonly 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.
1readonly 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.
1readonly XmlNode XmlDocument.nextSibling;
firstElementChild
XmlNode, returns the first child element node of the node
1readonly XmlNode XmlDocument.firstElementChild;
lastElementChild
XmlNode, returns the last child element node of the node
1readonly 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.
1readonly 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.
1readonly 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.
1String XmlDocument.textContent;
member function
load
The document is composed by parsing an XML/HTML string. Multilingualism is not supported.
1XmlDocument.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.
1XmlDocument.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
1XmlNodeList 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
2XmlNodeList 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
1XmlElement XmlDocument.getElementById(String id);
Call parameters:
- id: String, the id to be retrieved
Return results:
- XmlElement, with the specified id attribute in the node treeXmlElementnode
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
1XmlNodeList 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
1XmlElement XmlDocument.createElement(String tagName);
Call parameters:
- tagName: String, specifies the specified name of the element node
Return results:
- XmlElement, returns the newly createdXmlElementnode, with the specified label name
createElementNS
Creates an element node with the specified namespace
1
2XmlElement 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:
- XmlElement, returns the newly createdXmlElementnode, with the specified label name
createTextNode
Create text node
1XmlText XmlDocument.createTextNode(String data);
Call parameters:
- data: String, specifies the text of this node
Return results:
createComment
Create annotation node
1XmlComment XmlDocument.createComment(String data);
Call parameters:
- data: String, specifies the comment text of this node
Return results:
- XmlComment, returns the newly createdXmlCommentNode, the comment text is the specified data
createCDATASection
createXmlCDATASectionnode
1XmlCDATASection XmlDocument.createCDATASection(String data);
Call parameters:
- data: String, specifies that this node specifies CDATA data
Return results:
- XmlCDATASection, returns the newly createdXmlCDATASectionNode, the content is the specified data
createProcessingInstruction
createXmlProcessingInstructionnode
1
2XmlProcessingInstruction 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:
- XmlProcessingInstruction, the newly created ProcessingInstruction node
hasChildNodes
Query whether there are child nodes
1Boolean 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
1XmlDocument.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
1XmlNode 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:
- XmlNode, returns the copied node
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
1String 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
1String 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
2XmlNode XmlDocument.insertBefore(XmlNode newChild,
XmlNode refChild);
Call parameters:
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
2XmlNode XmlDocument.insertAfter(XmlNode newChild,
XmlNode refChild);
Call parameters:
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
1XmlNode 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
2XmlNode XmlDocument.replaceChild(XmlNode newChild,
XmlNode oldChild);
Call parameters:
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
1XmlNode 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.
1String 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.
1Value XmlDocument.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable