Using Dom in Javascript to operate Xml 1. Xml files
Ii. IXMLDOMDocument/DOMDocument Introduction
2.1 attributes
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text 3
2.1.5 attributes
2.1.6 nodeName
2.1.7 documentElement
2.1.8 nextSibling
2.1.9 childNodes
2.1.10 firstChild
2.1.11 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
Iii. Example
1. Xml file
C ++
20
1
2
Implementation Code in asp.net:
String str = Server. MapPath ("test1.xml ");
XmlTextWriter xmlWriter = new XmlTextWriter (str, null );
XmlWriter. Formatting = System. Xml. Formatting. Indented;
XmlWriter. WriteStartDocument ();
XmlWriter. 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 ();
Ii. IXMLDOMDocument/DOMDocument Introduction
2.1 attributes
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
Returns an object with a parsing error.
Important include parseError. errorCode, parseError. reason
If the path is incorrect during load, the system returns the error "the specified object is not found ".
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 from the following. Read-Only: If the xml of the book vertex is required," C ++ 20 1 2 ". If the Name is xml," C ++ "
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.
20
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:
C ++ 20 1 2 */
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 {
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 {
Var infoNode = nodeList. item (j );
Var cldNodes = infoNode. childNodes; // subnode set of the info Node
For (var k = 0; 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 {
Str + = scldNodes. item (t). nodeName + ":" + scldNodes. item (t). text + "";
}
// Result "k: 1"
Form1.test1. value = str;
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.