Javascript XML operation encapsulation class

Source: Internet
Author: User

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.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.