Traversing the node tree
You often need to loop through XML documents, such as when you need to extract the value of each element.
This process is called "traversing the Node tree"
Reading server-side XML (note the difference between different browser versions), using XML can enhance the scalability of the system, with only the modification of XML can be achieved to increase the reduction of functionality.
code is as follows |
copy code |
function LoadXMLDoc1 (dname) { if window. XMLHttpRequest) { xhttp=new xmlhttprequest (); }else{ xhttp=new activexobject ("Microsoft.XMLHTTP"); } Xhttp.open ("Get", dname,false); xhttp.send (""); return xhttp.responsexml; } |
2: Traversing the XML node
The getElementsByTagName () method can continue to call for a child node after a node is obtained, and returns an array (plus [0] for only one node)
. Childnodes[0].nodevalue) To obtain the value of a node, the value of the node is also a node (text node)
The code is as follows |
Copy Code |
function Test () { var xml=loadxmldoc1 ("Book.xml"); var bookss=xml.getelementsbytagname ("book"); for (Var i=0;i<bookss.length;i++) { document.write (Bookss[i].getelementsbytagname ("title") [0].childnodes[0].nodevalue); document.write ("</br>"); document.write (Bookss[i].getelementsbytagname ("author") [0].childnodes[0].nodevalue); document.write ("</br>"); document.write (Bookss[i].getelementsbytagname ("year") [0].childnodes[0].nodevalue); document.write ("</br>"); document.write (Bookss[i].getelementsbytagname ("Price") [0].childnodes[0].nodevalue); document.write ("</br>"); } } |
3:cdata node Value read
If there is a CDATA in an XML node, different browser methods are read at the same time, under IE nodevalue can get the value normally, read by Wholetext under FF and Chrome.
If a node browser is not supported, you can use if to determine.
The code is as follows |
Copy Code |
function Testcdata () { var xml=loadxmldoc1 ("Ceshi.xml"); var hightchartsopinions =xml.getelementsbytagname ("Hightchartsopinions"); for (var i=0;i VAR Wholetextdata=hightchartsopinions[i].childnodes[0].wholetext; var text=wholetextdata?wholetextdata:hightchartsopinions[i]. Childnodes[0].nodevalue document.write (text); document.write ("</br>"); } return false; } |
4: XML traversal through XPath
XPath can be very powerful for filtering XML nodes, and the following links contain commonly used XPath usages.
Http://www.w3school.com.cn/xpath/xpath_examples.asp
XPath reads XML different browsers also have different method implementations, where the callback function is used to process the return node.
The code is as follows |
Copy Code |
function XPath () { var xmlfilepath= "Book.xml"; var xpath= "/bookstore/book[price>45]"; var callback=function (Element) { document.write (Element.getelementsbytagname ("title") [0].childnodes[0].nodevalue); document.write ("</br>"); document.write (Element.getelementsbytagname ("author") [0].childnodes[0].nodevalue); document.write ("</br>"); document.write (Element.getelementsbytagname ("year") [0].childnodes[0].nodevalue); document.write ("</br>"); document.write (Element.getelementsbytagname ("Price") [0].childnodes[0].nodevalue); document.write ("</br>"); }; Handlerxml (Xmlfilepath,xpath,callback); }
function Handlerxml (xmlfilepath,xpath,callback) { var xml=loadxmldoc1 (Xmlfilepath); var action; if (window. ActiveXObject) { var nodes=xml.selectnodes (XPath); for (i=0;i<nodes.length;i++) { Callback (Nodes[i]); } }else if (document.implementation && document.implementation.createDocument) { var nodes=xml.evaluate (XPath, XML, NULL, Xpathresult.any_type, NULL); var result=nodes.iteratenext (); while (result) { Callback (result); Result=nodes.iteratenext (); } } } |
JavaScript parsing and generating XML
Using JS to parse and generate XML is generally the use of DOMDocument this object, the following simple list of its properties and methods:
Property:
Attributes a list of properties for storage nodes (read-only)
ChildNodes List of child nodes for storage nodes (read-only)
DataType returns the data type of this node
Definition of a node given in a DTD or XML schema (read-only)
Doctype Specify document Type node (read-only)
DocumentElement returns the root element of the document (Readable and writable)
FirstChild returns the first child node of the current node (read-only)
Implementation return Xmldomimplementation Object
LastChild returns the last child node of the current node (read-only)
NextSibling returns the next sibling node of the current node (read-only)
NodeName returns the name of the node (read-only)
NodeType returns the type of the node (read-only)
nodeTypedValue Storage node value (readable and writable)
NodeValue returns the text of the node (readable and writable)
Ownerdocument returns the root document containing this node (read-only)
ParentNode Return parent node (read only)
Parsed returns whether this node and its child nodes have been resolved (read-only)
Prefix return namespace prefix (read only)
PRESERVEWHITESPACE Specifies whether to leave blank (read-write)
PreviousSibling returns the previous sibling node of this node (read-only)
Text returns the textual content of this node and its descendants (readable and writable)
URL returns the URL of the most recently loaded XML document (read-only)
XML representation of the XML return node and its descendants (read-only)
Method:
appendchild adds a new child node for the current node, after the last child node
clonenode returns the copy of the current node
createattribute creates a new property
createcdatasection Create a CDATA segment that includes the given data
createcomment create a comment node
createdocumentfragment Create a DocumentFragment object
createelement Create an element node
createentityreference Create EntityReference Object
createnode creates a node for the given type, first name, and namespace
createporcessinginstruction create an operation instruction node
createtextnode Creates a text node that includes the given data
getelementsbytagname returns the collection of elements for the specified name
haschildnodes returns whether the current node has child nodes
InsertBefore Insert a child node before the specified node
load Import the XML document at the specified location
loadxml Import the XML document for the specified string
removechild deletes the specified child node from the list of child nodes. br> replacechild replaces the specified child node from the list of child nodes
save saves the XML file to a specified node
selectnodes a specified match to the node and returns a matching node list
selectsinglenode the specified match to the node and returns the first matching node
transformnode to convert the node and its descendants using the specified style sheet
transformNodeToObject Converts a node and its descendants to an object using the specified style sheet