What is xml dom?

Source: Internet
Author: User
Tags xslt

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.