Original article: What is xml dom? Translation: YuL
The Document Object Model (DOM) is an implementation method in MSXML to programmatically describe XML documents, fragments, nodes, and node sets, it also provides an application interface (API) for processing XML data ). As an XML description, it complies with the DOM specifications of W3C organizations. As a set of APIS, it is also a set of COM objects that implement interfaces, and can be used in XML applications. Many programming languages can write it, such as C/C ++, VB, VBS, and JS.
The following uses JS (JScript) program snippets to explain the basic process of xml dom programming.
* To program and process XML data, you must first create an xml dom object. For example, the following JS code snippet:
Var xmldoc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
* Then, you can load XML data from a file to a DOM object, as shown below:
Xmldoc. load ("file. xml ");
* You can also load XML data from an XML Stream to DOM objects. This XML Stream may come from other applications or be dynamically created:
StrXML = "<a> <a1> 1 </a1> <a2> 2 </a2> </a> ";
Xmldoc. loadXML (strXML );
* To navigate to a node of the XML document, you can use an XPath expression to call one of multiple methods of the DOM instance. For example:
Var node = xmldoc. selectSingleNode ("// a2 ");
// Insert a new element into the DOM object. You can set attributes and call object methods, or call its sub-object methods. For example, the following code snippet supplements an empty element <a3> as a new child element of <a>:
Xmldoc.doc umentElement. appendChild (xmldoc. createElement ("a3 "));
* To persist a DOM object, you must call the save method of the object:
Xmldoc. save ("new. xml ");
* To execute the XSL Transformation (XSLT) of the XML document, you can create other DOM objects to follow this XSLT style sheet and call the transformNode method of the DOM object for this XML document:
Var xslt = new ActiveXObject ("msxml2.DOMDocument. 3.0 ");
Xslt. load ("transform. xsl ");
StrXML = xmldoc. transformNode (xslt );
This is just a simple example to show you how to use DOM to process XML documents. More details will be discussed and described in the subsequent sections of this Guide.