CopyCode The 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 demoCopy 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.