JavaScript XML操作 封裝類

來源:互聯網
上載者:User

複製代碼 代碼如下: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\/>');
}

呵呵 寫的不怎樣 大家見笑 非常簡單的功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.