JavaScript的XML解析器 - xml2array()
xml2array()解析给定的XML文档并返回关联数组中的数据。使用
只需使用你觉得最方便的方法来加载XML文件,然后调用xml2array函数,将XML对象作为参数。XmlHttp.onreadystatechange = function() { if(XmlHttp.readyState == 4 && XmlHttp.status == 200) { var arr = xml2array(XmlHttp.responseXml); // Do what you want with 'arr' } })
;
代码
xml2array()
//See http://www.openjs.com/scripts/xml_parser/
var not_whitespace = new RegExp(/[^\s]/);//This can be given inside the funciton - I made it a global variable to make the scipt a little bit faster.
var parent_count;
//Process the xml data
function xml2array(xmlDoc,parent_count) {
var arr;
var parent = "";
parent_count = parent_count || new Object;
var attribute_inside = 0; /*:CONFIG: Value - 1 or 0
* If 1, Value and Attribute will be shown inside the tag - like this...
* For the XML string...
* <guid isPermaLink="true">http://www.bin-co.com/</guid>
* The resulting array will be...
* array['guid']['value'] = "http://www.bin-co.com/";
* array['guid']['attribute_isPermaLink'] = "true";
*
* If 0, the value will be inside the tag but the attribute will be outside - like this...
* For the same XML String the resulting array will be...
* array['guid'] = "http://www.bin-co.com/";
* array['attribute_guid_isPermaLink'] = "true";
*/
if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") {
if(xmlDoc.childNodes.length > 1) { //If its a parent
arr = new Object;
parent = xmlDoc.nodeName;
}
}
var value = xmlDoc.nodeValue;
if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value) {
if(not_whitespace.test(value)) {//If its a child
arr = new Object;
arr[xmlDoc.parentNode.nodeName] = value;
}
}
if(xmlDoc.childNodes.length) {
if(xmlDoc.childNodes.length == 1) { //Just one item in this tag.
arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:
} else { //If there is more than one childNodes, go thru them one by one and get their results.
var index = 0;
for(var i=0; i<xmlDoc.childNodes.length; i++) {//Go thru all the child nodes.
var temp = xml2array(xmlDoc.childNodes[i],parent_count); //:RECURSION:
if(temp) {
var assoc = false;
var arr_count = 0;
for(key in temp) {
if(isNaN(key)) assoc = true;
arr_count++;
if(arr_count>2) break;//We just need to know wether it is a single value array or not
}
if(assoc && arr_count == 1) {
if(arr[key]) { //If another element exists with the same tag name before,
// put it in a numeric array.
//Find out how many time this parent made its appearance
if(!parent_count || !parent_count[key]) {
parent_count[key] = 0;
var temp_arr = arr[key];
arr[key] = new Object;
arr[key][0] = temp_arr;
}
parent_count[key]++;
arr[key][parent_count[key]] = temp[key]; //Members of of a numeric array
} else {
parent_count[key] = 0;
arr[key] = temp[key];
if(xmlDoc.childNodes[i].attributes && xmlDoc.childNodes[i].attributes.length) {
for(var j=0; j<xmlDoc.childNodes[i].attributes.length; j++) {
var nname = xmlDoc.childNodes[i].attributes[j].nodeName;
if(nname) {
/* Value and Attribute inside the tag */
if(attribute_inside) {
var temp_arr = arr[key];
arr[key] = new Object;
arr[key]['value'] = temp_arr;
arr[key]['attribute_'+nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
} else {
/* Value in the tag and Attribute otside the tag(in parent) */
arr['attribute_' + key + '_' + nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
}
}
} //End of 'for(var j=0; j<xmlDoc. ...'
} //End of 'if(xmlDoc.childNodes[i] ...'
}
} else {
arr[index] = temp;
index++;
}
} //End of 'if(temp) {'
} //End of 'for(var i=0; i<xmlDoc. ...'
}
}
if(parent && arr) {
var temp = arr;
arr = new Object;
arr[parent] = temp;
}
return arr;
}
可写作一个xml2array.js文件。虽然它还有稍稍的不足,但是已经能基本完成任务。该文件还包括xjx库和转储函数(与PHP的print_r函数等效的JavaScript)的代码。
代码有点大(100行代码),所以不要把它放在这个页面 - 只是看看js文件的代码。你只需要xml2array()函数和顶部的两个全局变量 (有另外两个函数,忽略它们就好)。
问题:
脚本有一些问题 - 但这只是第一个版本,所以它是预期的。
作者只测试了以下浏览器中的脚本。你可以试着研究该脚本如何在其他浏览器工作。
- Linux中的Firefox 1.5
- Windows 1.5中的Firefox 1.5
- IE 6在Windows
- Mozilla在Linux
另一个问题是,当诸如XML版本(<?xml version="1.0"?>
)或样式表信息(<?xml-stylesheet type="text/xsl" href="softwares.xsl" ?>
)或一些其他额外信息之类的细节<!DOCTYPE programs SYSTEM "softwares.dtd">
存在于XML文件中时,脚本会遇到麻烦。作者猜测这不会是一个很大的问题,因为这个脚本旨在使用与XML格式的Ajax响应 - 所以我们可以放心地假设在其中不会有任何'样式表'信息;-)。如果您发现此问题的任何解决方案,可以联系作者 - 电子邮件是binnyva@gmail.com
作者希望可以得到这个脚本在Actionscript中工作的情况。作者不'知道'actionscript本身 - 但是知道javascript,并假设可以在Actionscript中编码,因为他们有非常相似的语法。不幸的是,脚本在Actionscript中失败 - 空格正则表达式的一些问题。如果有任何人知道行动脚本,请看看脚本,如果能在Flash中工作,请告诉作者binnyva@gmail.com。
Related Scripts
出处:http://www.openjs.com/scripts/xml_parser/