JavaScript XML Operations Encapsulation class _javascript Tips

Source: Internet
Author: User
Copy Code code 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;
To 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;
}
}
Get node byte point
This.getxmlarray=function (name) {
var keys = name.split ('. ');
var node = xmldoc.documentelement; Get root node
var Rtn = new Array ();
var n = 0;
for (var i=0; i<keys.length; i++) {
var childs = Node.childnodes; Get child node
var key = Keys[i];
for (var k=0; k<childs.length; k++) {
var child = Childs[k];
if (Child.nodename = = key) {//To determine whether the child nodes are compliant
if (i = = keys.length-1) {
Rtn[n] = child;
n++;
} else {
node = child;
Break
}
}
}
}
Node_xml=rtn;
return RTN;
}
Get node Content
This.getnodevalue=function (name)
{
Return This.getvalue (Xmldoc,name);
}
Get node contents of nodes according to node
This.getvalue=function (node, name)
{
var keys = name.split ('. ');
for (var i=0; i<keys.length; i++)
{
var childs = Node.childnodes; Get child node
var key = Keys[i];
for (var k=0; k<childs.length; k++)
{
var child = Childs[k];
if (Child.nodename = = key)
{//Determine if child nodes are compliant
if (child.childNodes.length = 1)
{
If there is no byte point, the return value
if (!window. ActiveXObject)
{
return childs[k].textcontent;
}
Else
{
Return Childs[k].text
}
}
Else
{
and sub nodes, continue profiling
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;
}
}

Invoke Method Demo
Copy Code code as follows:

var strxml= "<?xml version= ' 1.0 encoding= ' utf-8 '?><ekuy><user><name><cnname> ' Little Piggy </ cnname></name><age>27</age></user><user><name><cnname> Xiao Xia </ Cnname></name><age>26</age></user><user><name><cnname> Small 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 node

document.write (Xmlobj.getnodevalue (' ekuy.user.name.cnname '))/direct access to node content

The loop node gets the contents of the node 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\/> ');
}

Oh written not how everyone laughed very simple function
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.