one. Xml file
2. Introduction to IXMLDOMDocument/DOMDocument
2.1 Properties
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
3. Example
1. Xml file
Implement the code under 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();
2. Introduction to IXMLDOMDocument/DOMDocument
2.1 Properties
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
Returns an object when parsing the error.
The important ones are parseError.errorCode, parseError.reason
If the path is wrong when loading, the error "The system did not find the specified object" will be returned
2.1.2 async
Specifies whether asynchronous download is permitted
Whether asynchronous downloading is allowed, Boolean value
2.1.3 xml
Contains the XML representation of the node and all its descendants. Read-only.
This point and all derived below All information of the point, read-only. If the xml of the book point is requested, return "
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 this point and all points derived below, readable and writable
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 the collection 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
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
xml The root node of the xml above the 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
The first child node
2.1.11 lastChild
Returns the last child node
The last child Node
2.2 Methods
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 of 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 first matching node
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 IXMLDOMNodeList
All items that meet the conditions.
2.2.5 getElementsByTagName
Returns a collection of elements that have the specified name
Returns a collection of nodes that match the element name
2.2.6 hasChildNodes
Provides a fast way to determine whether a node has children
Determine whether it contains child nodes
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:
Form1.test1.value = root.nodeName; //The result is "book"
var att = root.attributes; //Get the set of all attributes at 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; //First child node Name
lNode = root.lastChild; //Last child Node info
nextSibling = fNode.nextSibling; //The next sibling node 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 the node with the element name "info"
for (var j= 0; j
var infoNode = nodeList.item(j);
var cldNodes = infoNode.childNodes; //Children of the info node Node set
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
var scldNodes = sNode.childNodes; //The set of child nodes 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;