Common functions for using Dom to operate Xml notes in Javascript

Source: Internet
Author: User
I. Xml files used in this note

 

<? Xml version = "1.0"?>

<Book level = "1">
<Name> c ++ </Name>

 

<Rice> 20 </Price>

<Info>
<K> 1 </k>

</Info>

<Info>

<K> 2 </k>
</Info>

 

</Book>

 

II. IXMLDOMDocument/DOMDocument Introduction 2.1 attribute 2.1.1 parseError

Returns an IXMLDOMParseError object that contains information about the last parsing error Returns an object when a parsing error occurs. Important
There is parseError. errorCode, parseError. reason. If the path is incorrect during load, the system will return the error "the specified object is not found ".
Asd

2.1.2 async

 

Specifies whether asynchronous download is permitted whether asynchronous download is allowed, Boolean Value

 

2.1.3 xml

Contains the XML representation of the node and all its descendants. Read-only.

All information of the vertex and all vertices derived below. Read-only if the xml of the book vertex is required, return "<book level =" 1 "> <Name> c ++ </Name> <Price> 20 </Price> <info> <k> 1 </k> </info> <k> 2 </k> </info> </book> ", if the Name is xml, "<Name> c ++ </Name>" is returned"

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 of the vertex and all derived points are readable and writable.

<Price> 20 </price>

The value of text is 20.

The text of "Name" node is "c ++"

2.1.5 attributes

Contains the list of attributes for this node

Returns the set of attributes.

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

Node name

"Name" indicates that the nodeName of the node is "Name", and "book" indicates that the nodeName of the node is "book"

2.1.7 documentElement

Contains the root element of the document

Xml Root Node

The above xml root node 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 child nodes.

2.1.10 firstChild

Contains the first child of the node

First subnode

2.1.11 lastChild

Returns the last child node

Last subnode

 

 

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 parameter path is a relative path on the server side.

2.2.3 selectSingleNode

Applies the specified pattern-matching operation to this node's context and returns the first matching Node

Returns the first matched item.

2.2.4 selectNodes

Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as ixmldomnodelist

All items that meet the conditions.

2.2.5 getElementsByTagName

Returns a collection of elements that have the specified name

Returns a set of nodes that match the element name.

2.2.6 hasChildNodes

Provides a fast way to determine whether a node has children

Determine whether a subnode exists

Return Value: bool Value

 

 

Iii. 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.doc umentelement; // Root Node
Form1.test1. value = root. xml;
/* The result is 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; // obtain the set of all attributes under this point.
VaR STR = "";
For (VAR I = 0; I <Att. length; I ++ ){
STR + = Att. Item (I). nodename + ":" + Att. Item (I). text;
}

Form1.test1. value = STR; // there is only one attribute, so the result is "level: 1"
Var fNode;
Var lNode;
Var nextSibling;
FNode = root. firstChild; // Name of the first subnode
LNode = root. lastChild; // The Last subnode info
NextSibling = fNode. nextSibling; // 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"); // search for a node whose element name is "info"
For (var j = 0; j <nodelist. length; j ++) // There are two Info Nodes
{
VaR infonode = nodelist. Item (j );
VaR cldnodes = infonode. childnodes; // subnode set of the info Node
For (var k = 0; k <cldnodes. length; k ++)
{
STR + = cldnodes. Item (k). nodename + ":" + cldnodes. Item (k). Text + "";
}
// Result "K: 1 K: 2"
}

STR = "";
VaR snode;
Snode = xmldoc. selectsinglenode ("// info"); // find the first one that matches "info"
Var scldNodes = sNode. childNodes; // subnode set of the info Node
For (var t = 0; t <scldNodes. length; t ++)
{
Str + = scldNodes. item (t). nodeName + ":" + scldNodes. item (t). text + "";
}
// Result "k: 1"
Form1.test1. value = STR;

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.