Using DOM to manipulate xml_javascript techniques in JavaScript

Source: Internet
Author: User
Tags dot net list of attributes
Read the XML data for a day, feeling csdn on this very meticulous. That is, a dot net to write an example of XML file, and JS read the example, is worth a look. (Source:http://blog.csdn.net/flypigluo)

A The XML file used in this note
Two Ixmldomdocument/domdocument Introduction
2. 1 Properties
2. 1. 1 parseerror
2. 1. 2 Async.
2. 1. 3 XML
2. 1. 4 Text3

2. 1. 5 attributes
2. 1. 6 nodename
2. 1. 7 documentelement
2. 1. 8 nextSibling
2. 1. 9 ChildNodes
2. 1. Ten FirstChild
2. 1. One Lashchild
2. 2 method
2. 2. 1 Loadxml
2. 2. 2 load
2. 2. 3 selectSingleNode
2. 2. 4 selectnodes
2. 2. 5 getElementsByTagName
2. 2. 6 HasChildNodes
Three Example


A The XML file used in this note
<?xml version= "1.0"?>
<book level= "1" > <Name>c++</Name>
<Price>20</Price>

<info>
<k>1</k>
</info>


<info>
<k>2</k>
</info>

</book>

To implement code under ASP.net:
String str = Server.MapPath ("Test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter (str,null);
xmlwriter.formatting = System.Xml.Formatting.Indented;
Xmlwriter.writestartdocument (); Mlwriter.writestartelement ("book");
Xmlwriter.writeattributestring ("Level", "1");
Xmlwriter.writeelementstring ("Name", "C + +");
Xmlwriter.writeelementstring ("Price", "20");
Xmlwriter.writestartelement ("info");
Xmlwriter.writeelementstring ("K", "1");
Xmlwriter.writeendelement ();
Xmlwriter.writestartelement ("info");
Xmlwriter.writeelementstring ("K", "2");
Xmlwriter.writeendelement ();
Xmlwriter.writeendelement ();
Xmlwriter.writeenddocument ();

Xmlwriter.close ();

Two Ixmldomdocument/domdocument Introduction
2. 1 Properties
2. 1. 1 parseerror
Returns an Ixmldomparseerror object so contains information about the last parsing error
Returns an object when parsing an error.
It's important to have Parseerror.errorcode,parseerror.reason
If the path is not correct at load time, it returns the error "the system did not find the specified object"
2. 1. 2 Async
Specifies whether asynchronous download is permitted
Whether to allow asynchronous downloads, Boolean values
2. 1. 3 XML

Contains the XML representation of the node and all its descendants. Read-only.
The point and all the information derived from all the points below, read only if the XML of the book point is required, return "<book level=" 1 ><name>c++</name><price>20</price ><info><k>1</k></info><info><k>2</k></info></book> ", If the XML of Name, returns "<Name>c++</Name>"
2. 1. 4 text
Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write
All node values for this point and all points derived from the following, readable and writable
<price>20</price>
Then text is 20
The text of the "Name" node is "C + +"
2. 1. 5 attributes
Contains the list of attributes for this node
Returns a collection of properties.
2. 1. 6 nodename
Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed string for all
Other node types. Read-only
The node name
The nodename of the "name" node is "name" and the nodename of the book node is "book"
2. 1. 7 documentelement
Contains the root element of the document
root node of XML
The root node of the above XML is "book"
2. 1. 8 nextSibling
Contains the next sibling of the node in the parent's child list. Read-only.
Next sibling node, read only
2. 1. 9 ChildNodes
Contains a node list containing the child nodes
All of the child nodes.
2. 1. Ten FirstChild
Contains the The Node
First child node
2. 1. One LastChild
Returns the last child node
Last child node



2. 2 method
2. 2. 1 Loadxml
Loads an XML document using the supplied string
2. 2. 2 load
Loads an XML document from the specified Locati
The path to the parameter is server-side and is a relative path
2. 2. 3 selectSingleNode
Applies the specified pattern-matching operation to this node's context and returns the ' s
Returns the first matching item
2. 2. 4 selectnodes
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as Ixmldomn Odelist
All items that meet the criteria.
2. 2. 5 getElementsByTagName
Returns A collection of elements that have the specified name
Returns a set of node that matches the element name
2. 2. 6 HasChildNodes
Provides a fast way to determine whether a node has children
Determine if child nodes are included
The return value is a bool value



Three Example
var xmldoc = new ActiveXObject ("msxml2.domdocument.3.0");
Xmldoc.async = false;
Xmldoc.load ("Test\\test1.xml");
if (xmldoc.parseerror.errorcode!=0)
{
var error = Xmldoc.parseerror;
Alert (Error.reason)
Return
}
var root = xmldoc.documentelement; Root node
Form1.test1.value = Root.xml;
/* The results are as follows:
<book level= "1" ><Name>c++</Name><Price>20</Price><info><k>1</k> </info><info><k>2</k></info></book>*/
Form1.test1.value = Root.nodename; The result is "book"
var att = root.attributes; Get a collection of all the attributes under that point
var str = "";
for (var i=0; i<att.length; i++)
{
STR + + Att.item (i). nodename+ ":" +att.item (i). text;
}
Form1.test1.value = str; Only one attribute, so the result is "level:1"
var FNode;
var Lnode;
var nextSibling;
FNode = Root.firstchild; First child node name
Lnode = Root.lastchild; Last child node info
nextSibling = fnode.nextsibling; The second sibling of the first child node name, that is, price
str = fnode.nodename + ":" + fnode.text; Result: "Name:c++"
str = lnode.nodename + ":" + lnode.text; The result is: "Info:2"
str = nextsibling.nodename + ":" + nextsibling.text; The result is: "Price:20"
var nodelist;
str = "";
NodeList = Xmldoc.selectnodes ("//info"); Find a node with an element named "info"
for (Var j=0 j<nodelist.length; j + +)//has two info nodes
{
var infonode = Nodelist.item (j);
var cldnodes = infonode.childnodes; Child node set of info node
for (var k=0; k<cldnodes.length; k++)
{
STR + + Cldnodes.item (k). NodeName + ":" + Cldnodes.item (k). Text + "";
}
Results "K:1 K:2"
}
str = "";
var Snode;
Snode = Xmldoc.selectsinglenode ("//info"); Find the first and "info" match.
var scldnodes = snode.childnodes; Child node set of info node
for (var t=0; t<scldnodes.length; t++)
{
str = Scldnodes.item (t). NodeName + ":" + scldnodes.item (t). Text + "";
}
Results "K:1"
Form1.test1.value = str;
Related Article

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.