Xml dom introduction and Examples
Author: chache chinaasp
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 used Programming Language Implementation 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 interface Program It 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
XML Parser must be used to process XML documents more skillfully. 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 ++, etc.
---- 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 following Code Load 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 a text string into XML Parser:
<Script language = "JavaScript">
VaR text = "<Note>"
TEXT = text + "<to> Tove </to> <from> Jani </from>"
TEXT = text + "TEXT = text + "<body> don't forget me this weekend! </Body>"
TEXT = text + "</Note>"
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. loadxml (text)
// ...... Processing the document goes here
</SCRIPT>
13. load XML into parser
<HTML>
<Body>
<Script language = "JavaScript">
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = "false"
Xmldoc. Load ("note. xml ")
Document. Write ("the first XML Element in the file contains :")
Document.write(xmldoc.doc umentelement. childnodes. Item (0). Text)
</SCRIPT>
</Body>
</Html>
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> (end)
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.