Copy codeThe Code is as follows:
Function XMLObject ()
{
This. isIE = true;
If (window. ActiveXObject) {isIE = true;} else {isIE = false ;}
Var node_xml;
Var xmlDoc;
If (isIE ){
XmlDoc = new ActiveXObject ("Msxml2.DOMDocument ");
}
Else {
If (document. implementation & document. implementation. createDocument ){
XmlDoc = document. implementation. createDocument ("", "", null );
}
}
XmlDoc. async = false;
XmlDoc. resolveExternals = false;
XmlDoc. validateOnParse = false;
XmlDoc. preserveWhiteSpace = true;
// Create an XML Object (XML string) from a string)
This. LoadXMLForString = function (XMLStr)
{
If (isIE)
{
XmlDoc. loadXML (XMLStr );
}
Else
{
Var oParser = new DOMParser ();
XmlDoc = oParser. parseFromString (XMLStr, "text/xml ");
}
}
// Create an XML file (file path) from an external file)
This. getXMLForURL = function (url)
{
// XmlDoc. load (url );
If (isIE ){
XmlDoc. load (url );
}
Else
{
XmlDoc = getXML (url). responseXML;
}
}
// Obtain the node byte
This. getXMLArray = function (name ){
Var keys = name. split ('.');
Var node = xmlDoc.doc umentElement; // get the root node
Var rtn = new Array ();
Var n = 0;
For (var I = 0; I <keys. length; I ++ ){
Var childs = node. childNodes; // obtain the subnode
Var key = keys [I];
For (var k = 0; k <childs. length; k ++ ){
Var child = childs [k];
If (child. nodeName = key) {// determines whether the child node matches
If (I = keys. length-1 ){
Rtn [n] = child;
N ++;
} Else {
Node = child;
Break;
}
}
}
}
Node_xml = rtn;
Return rtn;
}
// Obtain the node content
This. getNodeValue = function (name)
{
Return this. getValue (xmlDoc, name );
}
// Obtain the content of subnodes under the node based on the node
This. getValue = function (node, name)
{
Var keys = name. split ('.');
For (var I = 0; I <keys. length; I ++)
{
Var childs = node. childNodes; // obtain the subnode
Var key = keys [I];
For (var k = 0; k <childs. length; k ++)
{
Var child = childs [k];
If (child. nodeName = key)
{// Determine whether the Sub-nodes match
If (child. childNodes. length = 1)
{
// Return value if there is no byte point
If (! Window. ActiveXObject)
{
Return childs [k]. textContent;
}
Else
{
Return childs [k]. text
}
}
Else
{
// There are subnodes to continue the analysis
Node = child;
Break;
}
}
}
}
Return "";
}
Function GetXMLHTTP ()
{
Var xmlhttp;
If (window. ActiveXObject ){
Try {xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");}
Catch (e) {xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");}
} Else if (window. XMLHttpRequest)
{Xmlhttp = new XMLHttpRequest ();}
Return xmlhttp;
}
Function getXML (url ){
Var xmlHttp = GetXMLHTTP ();
If (xmlHttp! = Null ){
XmlHttp. open ("GET", url, false );
XmlHttp. send (null );
} Else {
Alert ("Your browser does not support XMLHTTP.11 ");
Return false;
}
Return xmlHttp;
}
}
Call method DEMO
Copy codeThe Code is as follows:
Var strXML = "<? Xml version = '1. 0' encoding = 'utf-8'?> <Ekuy> <user> <name> <cnname> Piglet </cnname> </name> <age> 27 </age> </user> <name> <cnname> Xiao Xia </cnname> </name> <age> 26 </age> </user> <name> <cnname> chapter </cnname> </name> <age> 25 </age> </user> </ekuy> ";
Var XMLObj = new XMLObject ();
XMLObj. LoadXMLForString (strXML)
// XMLObj. getXMLForURL ("test. xml ");
Var list = XMLObj. getXMLArray ('user'); // get the node
Document. write (XMLObj. getNodeValue ('ekuy. user. name. cnname'); // directly obtain the node content
// The cyclic node obtains the content of the subnode under the node.
For (var I = 0; I <list. length; I ++ ){
Var obj = list [I];
Document. write (XMLObj. getValue (obj, 'name. cnname '));
Document. write (XMLObj. getValue (obj, 'age '));
Document. write ('<br \/> ');
}
It's a simple function for everyone to laugh.