Xml dom introduction and Examples

Source: Internet
Author: User
Tags processing instruction xsl xsl file

 

1. Document Object Model (DOM)
Dom is the basis for programming HTML and XML documents. It defines the path for processing execution documents. Programmers can use Dom to add documents, locate document structures, and add and modify and delete document elements. W3C's important goal is to use Dom to provide a programming interface used on multiple platforms. W3C Dom is designed to be suitable for multiple platforms and can be usedProgramming LanguageImplementation Method.
2. node interface
XML parser is used to load XML documents to the cache. During file loading, you can use dom for retrieval and processing. Dom uses a tree structure to represent XML documents. The document element is the highest class of the tree. One or more child nodes are used to represent the branches of the tree.
Node interfaceProgramIt is usually used to read and write individual elements in the XML node tree. The child node attribute of the document element can be used to construct individual element nodes. XML parser is used to prove that the DOM in the Web supports traversing all the functions of the node tree, and can access nodes and their attributes through them, insert and delete nodes, and convert the node tree to XML.
All Microsoft XML Parser functions are officially recommended by W3C xml dom except load and loadxml functions (the formal dom does not include the standard function loading XML document ). 13 node types are supported by Microsoft XML parser. Common nodes are listed below:
Node Type example
Document Type <! Doctype Food System "food. DTD">
Processing Instruction <? XML version = "1.0"?>
Element <drink type = "beer"> Carlsberg </drink>
Attribute type = "beer"
Text Carlsberg


3. use XML Parser
to process XML documents more skillfully, you must use XML parser. Microsoft XML parser is a COM component included in iis5.0. Once iis5.0 is installed, Parser can use scripts in HTML documents and ASP files.
Microsoft xmldom parser supports the following programming modes:
---- supports JavaScript, VBScript, Perl, VB, Java, c ++ and so on
---- supports W3C XML 1.0 and xml dom
---- supports DTD and Validation
if Javascript in ie5.0 is used, you can use the following XML Document Object:
var xmldoc = new activexobject ("Microsoft. xmldom ")
If VBScript is used, you can use the following XML Document Object:
set xmldoc = Createobject (" Microsoft. xmldom ")
If ASP is used, you can use the following XML Document Object:
set xmldoc = server. createobject ("Microsoft. xmldom ")

4. Load an XML file to parser
The followingCodeLoad the existing XML file into XML Parser:
<Script language = "JavaScript">
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note. xml ")
// ...... Processing the document goes here
</SCRIPT>
The first line of the script adds a Microsoft XML Parser instance, and the third line loads the XML file named "note. xml" into the parser. The second line ensures that the parser performs the next step after the document is loaded.

5. parseerror object
When opening an XML document, XML Parser generates error code and contains the error code, error text, error row number, and other information in the parseerror object.

6. File Error
The following example will try to load a non-existent file and generate the corresponding error code:
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("ksdjf. xml ")
Document. Write ("<br> Error Code :")
Document. Write (xmldoc. parseerror. errorcode)
Document. Write ("<br> error reason :")
Document. Write (xmldoc. parseerror. Reason)
Document. Write ("<br> error line :")
Document. Write (xmldoc. parseerror. Line)

7. xml Error
The following uses an incorrect format to load the XML document,
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note_error.xml ")
Document. Write ("<br> Error Code :")
Document. Write (xmldoc. parseerror. errorcode)
Document. Write ("<br> error reason :")
Document. Write (xmldoc. parseerror. Reason)
Document. Write ("<br> error line :")
Document. Write (xmldoc. parseerror. Line)

8. parseerror attributes
Attribute description:
Error Code of Long Integer type returned by errorcode
Cause of string errors returned by reason
Line returns a long integer error line number.
Linepos returns a long integer with an incorrect row number.
The error cause of the string type returned by srctext
URL return URL loading document pointer
Filepos returns the location of a long integer error file

9. traverse the node tree
A common method for retrieving XML documents is to traverse the node tree and Its element values. The program code for traversing the node tree written in VBScript is as follows:
Set xmldoc = Createobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note. xml ")
For each X in xmldoc.doc umentelement. childnodes
Document. Write (X. nodename)
Document. Write (":")
Document. Write (X. Text)
Next

10. Provide HTML format for XML files
XML separates HTML documents from their data. By using XML parser in the browser, HTML pages can be constructed as static documents to provide dynamic data through JavaScript. The following example uses JavaScript to read XML documents and write XML data into HTML elements:
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note. xml ")
Nodes = xmldoc.doc umentelement. childnodes
To. innertext = nodes. Item (0). Text
From. innertext = nodes. Item (1). Text
Header. innertext = nodes. Item (2). Text
Body. innertext = nodes. Item (3). Text

11. Access XML elements by name
The following example uses JavaScript to read XML documents and write XML data into HTML elements:
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note. xml ")
Document. Write (xmldoc. getelementsbytagname ("from"). Item (0). Text)

12. load plain XML text into parser
the following code loads the text string into XML Parser:

13. load XML to parser





Traverse the XML node tree:
<HTML>
<Body>
<Script language = "VBScript">
TXT = "Document. Write (txt)
Set xmldoc = Createobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note. xml ")
For each X in xmldoc.doc umentelement. childnodes
Document. Write ("<B>" & X. nodename & "</B> ")
Document. Write (":")
Document. Write (X. Text)
Document. Write ("<br> ")
Next
</SCRIPT>
</Body>
</Html>

Load XML to HTML
<HTML>
<Head>
<Script language = "JavaScript"
For = "window" event = "onLoad">
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note. xml ")
Nodes = xmldoc.doc umentelement. childnodes
To. innertext = nodes. Item (0). Text
From. innertext = nodes. Item (1). Text
Header. innertext = nodes. Item (2). Text
Body. innertext = nodes. Item (3). Text
</SCRIPT>
<Title> HTML using XML data </title>
</Head>
<Body bgcolor = "yellow">
<H1> refsnes data internal note <B> to: </B> <span id = "to"> </span>
<Br>
<B> from: </B> <span id = "from"> </span>
<HR>
<B> <span id = "Header"> </span> </B>
<HR>
<Span id = "body"> </span>
</Body>
</Html>

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>


straight talk about computers
19.99
Lars Peterson


Create a reference to the second element:
set thenode into dsobooks.xmldocument.doc umentelement. childnodes (1)
● root node: thenode. ownerdocument returns the document node, pointing to the XML document itself;
● sibling node: thenode. previussibling returns 1st elements, thenode. nextsibling returns 3rd elements;
● parent node: thenode. parentnode returns the element;
● subnode: thenode. firstchild returns the element, thenode. lastchild returns the element, thenode. childnodes Element. 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 node reference, you can read the node information:
● node type: thenode. nodetype. In this example, the document object type is 9, the element type is 1, and the attribute type is 2.
● node name: thenode. nodename, in this example, book;
● node value: thenode. nodevalue. In this example, It is null. For ATTR nodes, the returned value is the property value, and for element nodes, the returned value is null.
In MSXML, some additional methods and attributes are provided for the Node object:
● nodetypestring: displays the node type in string mode, such as thenode. the result of nodetypestring is "element".
● text: displays the text of the current node and all its subnodes.
● XML: obtains XML document data, usually all content 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)

1. Dom parsing errors
Dom may cause various errors when parsing XML documents. You can learn the possible causes and related information of Errors Based on the attributes of the parseerror object.
Common attributes and their meanings are shown in the following table:
Attribute description
Error Code
The absolute character position of the filepos error in the document
Line number of the row where the line error is located
The character position of the line in which the linepos error is located
Cause of reason Error
The row where the srctext error is located Source code
The URL of the latest XML document containing resolution errors


2. Access elements and attributes in the DOM tree
Dom also provides many methods for searching nodes. The search-based methods include:
● Search for elements by Tag name;
● Search nodes in XSL mode;
● Use the SET index to search nodes.
Taking books. XML as an example, the getelementsbytagname method in the document object is used to search for elements in the full text range based on the tag name in the parameter. The returned value is a nodelist object:
Set Doc = dsodetails. xmldocument
Set authors = Doc. getelementsbytagname ("author ")
The preceding query results contain all four authors in the document. If you call the getelementsbytagname method in the element object, except for narrowing the search scope to all subsequent nodes of the element, the other conditions are the same.
All types of nodes carry the selectnodes method. The unique parameter of this method is the XSL Pattern Rule, and the return value is the result set that matches the rule. To call this method, you can use the XSL pattern matching policy to find nodes. For example:
Set rootnode = doc.doc umentelement
Set cheapbooks = rootnode. selectnodes ("// book [price <10]")
In this example, all <book> elements whose prices are less than 10 RMB are returned. In addition, the selectsinglenodes method in the node has the same usage as selectnodes, but the returned result is the first node that meets the search criteria.

for element nodes, there are two methods to obtain the element Tag Name: anyelement. nodename and anyelement. tagname. The former is the attribute of the Node object, and the latter is the attribute of the element object.
if you want to obtain the text content in an element, for example, 9.95
, the nodevalue attribute in the element object is incorrect, the returned result is null instead of the expected 9.95. All elements that contain text content contain a subnode of the text type. Therefore, only the nodevalue attribute in the Text object can access the text content.
the procedure for adding an element is as follows:
● create a text node and assign a value;
● create an element node;
● mount the text node to the element node, as its sub-node;
● Insert the element node to the appropriate location of the XML document.

to delete or replace an element node, you must first locate the operation object and then execute the removechild method and replaceChild method of the parent node of the object node.
operations on the ATTR node are in principle the same as those on the element node. The ATTR object also inherits various methods and attributes of the Node object, and MSXML also provides the name and value attributes, allowing you to directly access the attribute information. In addition, you can access attributes through the relevant methods of the element to which the attribute belongs. For example, you can use the getattribute and setattribute methods to read or modify the attribute values, or use the getattributenode method to directly return the ATTR object.
the most direct way to create a new property is to use the setattribute method in the element object. You can also set the attribute value using the createattribute method in the Document Object, and then add the new node to the DOM tree using the setattributenode method in the element object. Similarly, the most direct method to delete an attribute is to call the removeattribute method in element. Another solution is to first use the getattributenode method to locate the operation object and then perform the removeattributenode operation.
from the above introduction, we can see that due to the inheritance relationship between nodes and the rich interfaces provided by various types of nodes, you can easily find an object operation solution that suits your needs.
3. Dom display function
DOM technology can also be used to display XML data. The XSL style is a single-sided conversion of XML documents, which is used to convert the display format. Therefore, there are still some shortcomings in the display function:
● it is difficult to complete complex processing of XML data, such as converting all English letters into uppercase letters, intercepting strings of the specified length, and ignoring some specific punctuation marks;
● it is not easy to calculate values in XML data.
● an XSL is usually used on an XML document statically, data in multiple XML documents cannot be merged and converted into an output result using one XSL.
Dom can effectively solve the above problems

from: http://www.cnblogs.com/dujun0618/articles/688058.html

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.