Operate data (on) through DOM)

Source: Internet
Author: User
Tags xsl file
1. DOM tree
All types of XML parsers require that the processing object be a "well-formed" XML document, and some can also be validated based on the DTD or XML schema, Dom (Document Object Model) the parser parses the XML document at one time and generates an object tree in memory to describe the document.
Dom is a platform-and language-independent interface that allows programs and scripts to dynamically access and modify the content, structure, and type of a document. It defines a series of objects and methods to perform various random operations on DOM tree nodes:
● Document Object: as the highest node of the tree, the document object is the portal for operations on the entire document.
● Element and ATTR objects: These node objects are mapped to a certain part of the document. The Node grading level exactly reflects the structure of the document.
● Text object: As a subnode of the element and ATTR objects, the text object expresses the text content of the element or attribute. The text node no longer contains any child nodes.
● SET index: Dom provides several set indexes to traverse nodes in the specified mode. Index parameters start from 0.
All nodes in the DOM tree are inherited from the Node object. The Node object defines some basic attributes and methods. These methods can be used to traverse the tree. At the same time, the node name and value can be known and its type can be determined based on the attributes.
With Dom, developers can dynamically create XML, traverse documents, and add/delete/modify documents. The APIS provided by Dom are not related to programming languages. Therefore, for some interfaces that are not clearly defined in Dom standards, different parser implementation methods may also be different. For ease of description, examples in this article use the MSXML Dom scheme and use VB script to write code.
2. DOM tree structure
After the document object is created, it can be associated with the XML document or data island. The data island loading method is to assign the data island ID to the Document Object:
<XML id = "dsodetails" src = "books. xml"> </XML>
Set Doc = dsodetails. xmldocument
Document loading is divided into three steps:
1. Use the Createobject method to create a analyzer instance;
2. Set the async attribute to false to disable asynchronous loading. After the file is loaded, the control will be returned to the calling process. If you want to obtain the file loading status, you can read the readystate attribute value;
3. Load the specified document using the load method.
Set Doc = Createobject ("Microsoft. xmldom ")
Doc. async = false
Doc. Load "Books. xml"
Xml dom also provides a loadxml method to load XML strings into the DOM tree. You only need to directly use XML strings as parameters of this method.
3. DOM tree access
After the document is loaded, you can use the documentelement attribute to access the root element:
Set rootnode = doc.doc umentelement
Once a reference to a node (such as the root node) in the DOM tree is established, an appropriate method can be called to traverse the hierarchy between nodes.
The following uses books. XML as an example to describe how to use it:
<XML id = "dsobooks">
<? XML version = "1.0"?> <Booklist> <book>
<Title> the gourmet microwave </title>
<Price> 9.95 </price>
<Author> Charlotte M. Cooper </author>
<Author> Shelley B. Burke </author>
<Author> Regina P. Murphy </author>
</Book> <book>
<Title> sushi, anyone? </Title>
<Price> 14.99 </price> </book> <book>
<Title> straight talk about computers </title> <price> 19.99 </price>
<Author> Lars Peterson </author>
</Book> </booklist> </XML>
Create a reference to the second <book> element:
Set thenode mongodsobooks.xmldocument.doc umentelement. childnodes (1)
● Root node: thenode. ownerdocument returns the document node, pointing to the XML document itself;
● Sibling node: 1st <book> elements are returned for thenode. previussibling, and 3rd <book> elements are returned for thenode. nextsibling;
● Parent node: thenode. parentnode returns the <booklist> element;
● Subnode: thenode. firstchild returns the <title> element, and thenode. lastchild returns the <price> element. thenode. childnodes returns the subnode set, including all elements under sushi. The node count starts from 0, that is, the result of thenode. childnodes (0) is the same as that of thenode. firstchild.
After obtaining the reference of a node, you can read the relevant information of the node:
● Node type: thenode. nodetype. In this example, it is 1. The document object type is 9, the element type is 1, and the attribute type is 2;
● Node name: thenode. nodename. This example is book;
● Node value: thenode. nodevalue. In this example, the value is null. For ATTR nodes, the returned value is the attribute value, and for element nodes, the returned value is null.
In MSXML, some additional methods and attributes are provided for node objects:
● Nodetypestring: the node type is displayed as a string. For example, the result of thenode. nodetypestring is "element ";
● Text: displays the text of the current node and all its subnodes;
● XML: Get XML document data, usually starting from the root element.
4. Dynamic conversion of XML format
After learning XSL, we can use style sheets to convert XML documents. However, this process is static, that is, when writing code, the XSL file that has been specified for the XML file cannot be changed during the program running. With Dom, we can achieve dynamic conversion of XML format, that is, when the program is running, we load the XSL and convert the XML document.
The steps for loading XSL into DOM objects are basically the same as the loading process of XML documents (XSL itself is an XML document ):
Set stylesheet = Createobject ("Microsoft. xmldom ")
Stylesheet. async = false
Stylesheet. Load "transformdetails. XSL"
Dom provides two functions for this conversion. The object can be any node in the tree. In this way, you can convert the format of any part of the DOM tree.
● Transformnodetoobject method: This method requires two parameters. The first parameter points to the XSL file, and the second parameter stores the converted XML data nodes. For example:
Set targetnode = Createobject ("Microsoft. xmldom ")
Srcnode. transformnodetoobject stylesheet, targetnode
● Transformnode method: This method only requires one parameter to specify the XSL file. In the following example, the source node is converted to a string variable STR:
STR = srcnode. transformnode (stylesheet)
The method described above can be implemented on the server and has more practical significance: the server can use different style sheets for Conversion Based on different identities of the client, the same XML document is presented to different users in different forms!

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.