一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)
来源:互联网 发布:网络语废鱼是什么意思 编辑:程序博客网 时间:2024/06/03 05:08
一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)
一个用来将xml文件转换成对象的插件
xml文件如下
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<content>
<plugins>TreeView</plugins>
<plugins>ListGrid</plugins>
<plugins>ListBox</plugins>
</content>
<pool>1</pool>
</root>
生成的对象为:
调用
var xml = $.loadXML("PluginConf.xml");
var obj = $(xml).toObject().get(0);
alert(obj.pool);
以下是源码下载
一个用来将xml文件转换成对象的插件
xml文件如下
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<content>
<plugins>TreeView</plugins>
<plugins>ListGrid</plugins>
<plugins>ListBox</plugins>
</content>
<pool>1</pool>
</root>
生成的对象为:
调用
var xml = $.loadXML("PluginConf.xml");
var obj = $(xml).toObject().get(0);
alert(obj.pool);
以下是源码下载
/**
* 转换xml为对象形式
* @return {Object}
* @param {XMLHttpRequest} elXML
*/
$.fn.toObject = function (){
if (this==null) return null;
var retObj = new Object;
buildObjectNode(retObj,/*jQuery*/this.get(0));
return $(retObj);
function buildObjectNode(cycleOBJ,/*Element*/elNode){
/*NamedNodeMap*/
var nodeAttr=elNode.attributes;
if(nodeAttr != null){
if (nodeAttr.length&&cycleOBJ==null) cycleOBJ=new Object;
for(var i=0;i<nodeAttr.length;i++){
cycleOBJ[nodeAttr[i].name]=nodeAttr[i].value;
}
}
var nodeText="text";
if (elNode.text==null) nodeText="textContent";
/*NodeList*/
var nodeChilds=elNode.childNodes;
if(nodeChilds!=null){
if (nodeChilds.length&&cycleOBJ==null) cycleOBJ=new Object;
for(var i=0;i<nodeChilds.length;i++){
if (nodeChilds[i].tagName!=null){
if (nodeChilds[i].childNodes[0]!=null&&nodeChilds[i].childNodes.length<=1&&(nodeChilds[i].childNodes[0].nodeType==3||nodeChilds[i].childNodes[0].nodeType==4)){
if (cycleOBJ[nodeChilds[i].tagName]==null){
cycleOBJ[nodeChilds[i].tagName]=nodeChilds[i][nodeText];
}else{
if (typeof(cycleOBJ[nodeChilds[i].tagName])=="object"&&cycleOBJ[nodeChilds[i].tagName].length){
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length]=nodeChilds[i][nodeText];
}else{
cycleOBJ[nodeChilds[i].tagName]=[cycleOBJ[nodeChilds[i].tagName]];
cycleOBJ[nodeChilds[i].tagName][1]=nodeChilds[i][nodeText];
}
}
}else{
if (nodeChilds[i].childNodes.length){
if (cycleOBJ[nodeChilds[i].tagName]==null){
cycleOBJ[nodeChilds[i].tagName]=new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName],nodeChilds[i]);
}else{
if (cycleOBJ[nodeChilds[i].tagName].length){
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length]=new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length-1],nodeChilds[i]);
}else{
cycleOBJ[nodeChilds[i].tagName]=[cycleOBJ[nodeChilds[i].tagName]];
cycleOBJ[nodeChilds[i].tagName][1]=new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][1],nodeChilds[i]);
}
}
}else{
cycleOBJ[nodeChilds[i].tagName]=nodeChilds[i][nodeText];
}
}
}
}
}
}
}
/**
* @return {Element}
* @param {String} _url
*/
$.loadXML = function (_url){
var ret;
$.ajax({
type:"get",
url:_url,
async:false,
dataType:"xml",
success:function(xml){
ret = xml;
}
});
return ret.documentElement;
}
* 转换xml为对象形式
* @return {Object}
* @param {XMLHttpRequest} elXML
*/
$.fn.toObject = function (){
if (this==null) return null;
var retObj = new Object;
buildObjectNode(retObj,/*jQuery*/this.get(0));
return $(retObj);
function buildObjectNode(cycleOBJ,/*Element*/elNode){
/*NamedNodeMap*/
var nodeAttr=elNode.attributes;
if(nodeAttr != null){
if (nodeAttr.length&&cycleOBJ==null) cycleOBJ=new Object;
for(var i=0;i<nodeAttr.length;i++){
cycleOBJ[nodeAttr[i].name]=nodeAttr[i].value;
}
}
var nodeText="text";
if (elNode.text==null) nodeText="textContent";
/*NodeList*/
var nodeChilds=elNode.childNodes;
if(nodeChilds!=null){
if (nodeChilds.length&&cycleOBJ==null) cycleOBJ=new Object;
for(var i=0;i<nodeChilds.length;i++){
if (nodeChilds[i].tagName!=null){
if (nodeChilds[i].childNodes[0]!=null&&nodeChilds[i].childNodes.length<=1&&(nodeChilds[i].childNodes[0].nodeType==3||nodeChilds[i].childNodes[0].nodeType==4)){
if (cycleOBJ[nodeChilds[i].tagName]==null){
cycleOBJ[nodeChilds[i].tagName]=nodeChilds[i][nodeText];
}else{
if (typeof(cycleOBJ[nodeChilds[i].tagName])=="object"&&cycleOBJ[nodeChilds[i].tagName].length){
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length]=nodeChilds[i][nodeText];
}else{
cycleOBJ[nodeChilds[i].tagName]=[cycleOBJ[nodeChilds[i].tagName]];
cycleOBJ[nodeChilds[i].tagName][1]=nodeChilds[i][nodeText];
}
}
}else{
if (nodeChilds[i].childNodes.length){
if (cycleOBJ[nodeChilds[i].tagName]==null){
cycleOBJ[nodeChilds[i].tagName]=new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName],nodeChilds[i]);
}else{
if (cycleOBJ[nodeChilds[i].tagName].length){
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length]=new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length-1],nodeChilds[i]);
}else{
cycleOBJ[nodeChilds[i].tagName]=[cycleOBJ[nodeChilds[i].tagName]];
cycleOBJ[nodeChilds[i].tagName][1]=new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][1],nodeChilds[i]);
}
}
}else{
cycleOBJ[nodeChilds[i].tagName]=nodeChilds[i][nodeText];
}
}
}
}
}
}
}
/**
* @return {Element}
* @param {String} _url
*/
$.loadXML = function (_url){
var ret;
$.ajax({
type:"get",
url:_url,
async:false,
dataType:"xml",
success:function(xml){
ret = xml;
}
});
return ret.documentElement;
}
- 一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)
- 一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)
- 一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)
- Jquery将一个object对象转换成json字符串
- json(javascript Object notation )轻量级的javascript传输对象
- json字符串与对象的相互转换-javascript/jQuery
- json字符串与对象的相互转换-javascript/jQuery
- javaScript对象(object)转换为JSON格式
- 之前写的一个XML工具类,包括将JSON对象转换为XML对象
- 每天一个小知识点19(Javascript对象转换成json的函数)
- xml,json,对象 之间的相互转换
- XML,Object,Json转换之浅析Xstream的使用
- XML,Object,Json转换之浅析Xstream的使用
- 将基于spring的json字符串转换为Object对象
- json字符串与Object对象间的转换
- javascript 的对象(Object)
- javascript的Object对象
- Javascript / jQuery 笔记 DOM 对象和jQuery对象的转换
- TestCreateContact
- 内存对齐详解
- FORALL中的values of和indices of的区别
- 安装 memcache
- Android apk 真机安装方法
- 一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)
- .Net, XNA, Kinect中各种图的转换
- CSDN的心声
- 17个新手常见Python运行时错误
- c++如何生成release版本
- Lua-计算表中有多少个元素-#table的用法
- Cocos2d-X游戏工具开发之一:将Cocos2d-X嵌入MFC的子窗体方法讲解
- Java7遍历文件夹
- 《网络经济与企业管理》思维导图总结