Home > Web Front-end > JS Tutorial > JavaScript XML operation encapsulation class_javascript skills

JavaScript XML operation encapsulation class_javascript skills

WBOY
Release: 2016-05-16 18:51:05
Original
1012 people have browsed it
Copy code 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 XML object (XML string) from string
this.LoadXMLForString=function(XMLStr)
{
if(isIE)
{
xmlDoc.loadXML(XMLStr);
}
else
{
var oParser = new DOMParser();
xmlDoc = oParser.parseFromString(XMLStr,"text/xml");
}
}
//Create XML file from external file (file path)
this.getXMLForURL=function(url)
{
//xmlDoc.load(url);
if (isIE){
xmlDoc.load(url);
}
else
{
xmlDoc = getXML(url).responseXML;
}
}
//Get node bytes Point
this.getXMLArray=function (name) {
var keys = name.split('.');
var node = xmlDoc.documentElement; // Get the root node
var rtn = new Array();
var n = 0;
for(var i=0; ivar children = node.childNodes; // Get child node
var key = keys[i];
for(var k=0; kvar child = childrens[k];
if(child.nodeName == key) { // Determine whether the child node conforms to
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 the content of the child node under the node according to the node
this.getValue=function(node , name)
{
var keys = name.split('.');
for(var i=0; i{
var children = node.childNodes; // Get child nodes
var key = keys[i];
for(var k=0; k{
var child = childrens[ k];
if(child.nodeName == key)
{ // Determine whether the child node conforms to
if(child.childNodes.length == 1)
{
// If There is no byte point, return value
if(!window.ActiveXObject)
{
return children[k].textContent;
}
else
{
return children[ k].text
}
}
else
{
// There are also child nodes, continue to analyze
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 the code The code is as follows:

var strXML=" Little Pig27 Xiaoxia26Xiaozhang 25";

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'));//Get the node content directly

//Loop the node to get the child node content under the node
for(var i=0; ivar obj = list[i];
document.write(XMLObj.getValue(obj, 'name.cnname'));
document.write(XMLObj.getValue(obj, 'age'));
document.write('
');
}

Haha, it’s not very well written. It’s a very simple function that everyone laughs at.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template