JavaScript XML操作 封装类

复制代码 代码如下:


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;
//从字符串创建XML对象(XML字符串)
this.LoadXMLForString=function(XMLStr)
{
if(isIE)
{
xmlDoc.loadXML(XMLStr);
}
else
{
var oParser = new DOMParser();
xmlDoc = oParser.parseFromString(XMLStr,"text/xml");
}
}
//从外部文件创建XML文件(文件路径)
this.getXMLForURL=function(url)
{
//xmlDoc.load(url);
if (isIE){
xmlDoc.load(url);
}
else
{
xmlDoc = getXML(url).responseXML;
}
}
//获得节点字节点
this.getXMLArray=function (name) {
var keys = name.split('.');
var node = xmlDoc.documentElement; // 得到根节点
var rtn = new Array();
var n = 0;
for(var i=0; i<keys.length; i++) {
var childs = node.childNodes; // 得到子节点
var key = keys[i];
for(var k=0; k<childs.length; k++) {
var child = childs[k];
if(child.nodeName == key) { // 判断子节点是否符合
if(i == keys.length-1) {
rtn[n] = child;
n++;
} else {
node = child;
break;
}
}
}
}
node_xml=rtn;
return rtn;
}
//获得节点内容
this.getNodeValue=function(name)
{
return this.getValue(xmlDoc,name);
}
//根据节点获得节点下子节点内容
this.getValue=function(node, name)
{
var keys = name.split('.');
for(var i=0; i<keys.length; i++)
{
var childs = node.childNodes; // 得到子节点
var key = keys[i];
for(var k=0; k<childs.length; k++)
{
var child = childs[k];
if(child.nodeName == key)
{ // 判断子节点是否符合
if(child.childNodes.length == 1)
{
// 如果没有字节点,返回值
if(!window.ActiveXObject)
{
return childs[k].textContent;
}
else
{
return childs[k].text
}
}
else
{
// 还有子节点,继续分析
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;
}
}


调用方法DEMO

复制代码 代码如下:


var strXML="<?xml version='1.0' encoding='utf-8'?><ekuy><user><name><cnname>小猪</cnname></name><age>27</age></user><user><name><cnname>小霞</cnname></name><age>26</age></user><user><name><cnname>小章</cnname></name><age>25</age></user></ekuy>";

var XMLObj=new XMLObject();
XMLObj.LoadXMLForString(strXML)
//XMLObj.getXMLForURL("test.xml");

var list = XMLObj.getXMLArray( 'user');//获得节点

document.write(XMLObj.getNodeValue('ekuy.user.name.cnname'));//直接获得节点内容

//循环节点获得节点下子节点内容
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\/>');
}


呵呵 写的不怎样 大家见笑 非常简单的功能

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwgys.html