在 Javascript 中将 xml 转换为 json
convert xml to json in Javascript
我有以下javascript函数(我从Stack Overflow获得的),它将XML转换为JSON:
function xmlToJson(xml) {
try {
var obj = {};
if (xml.nodeType == 1) {
if (xml.attributes.length > 0) {
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj[attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) {
obj = xml.nodeValue;
}
if (xml.hasChildNodes()) {
for (var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof (obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if (typeof (obj[nodeName].push) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
console.log(JSON.stringify(obj));
return obj;
} catch (e) {
alert(e.message);
}
}
我想要的是当 xml 节点至少具有单个子节点并且它还具有父节点时将其作为数组 ([]) 返回。在此代码中,如果 xml 节点具有单个子节点,则返回 map ({}),但对于多个子节点,则返回映射 ({})。
例如,我想要 XML
<pnode attr1="abc">
<cnode attr2="xyz"></cnode>
</pnode>
要转换为 JSON
{
"pnode": {
"attr1": "abc"
},
"cnode": [
{"attr2": "xyz"}
]
}
澄清了您想要实现的目标,这里有一个算法。我会留下我的另一个答案,因为我仍然认为最明智的选择是不玩结构
function flattenNodes(node, isChild) {
var obj = {}, obj2, i, key, attributes = {};
if (node.attributes && node.attributes.length)
for (i = 0; i < node.attributes.length; ++i)
attributes[node.attributes[i].nodeName] = node.attributes[i].nodeValue;
if (!isChild)
obj[node.nodeName] = attributes;
else {
if (!obj.hasOwnProperty(node.nodeName))
obj[node.nodeName] = [];
else if (!(obj[node.nodeName] instanceof Array))
obj[node.nodeName] = [obj[node.nodeName]];
obj[node.nodeName].push(attributes);
}
attributes = null; // free
if (node.childNodes && node.childNodes.length)
for (i = 0; i < node.childNodes.length; ++i) {
if (node.childNodes[i].nodeType === 3) continue; // skip text node
obj2 = flattenNodes(node.childNodes[i], 1); // recurse
for (key in obj2) // merge
if (obj2.hasOwnProperty(key))
if (!obj.hasOwnProperty(key)) {
obj[key] = obj2[key];
} else {
if (!(obj[key] instanceof Array))
obj[key] = [obj[key]];
obj[key] = obj[key].concat(obj2[key]);
}
}
return obj;
}
节点 root_node
上的用法示例
var root_node;
root_node = new DOMParser().parseFromString(
'<pnode attr1="abc"><cnode attr2="xyz"></cnode></pnode>',
'text/xml'
).documentElement;
var o = flattenNodes(root_node); // create
JSON.stringify(o); // to JSON
// {"pnode":{"attr1":"abc"},"cnode":[{"attr2":"xyz"}]}
如果你有<foo bar="baz"><foo hello="world"></foo></foo>
形式的XML,第一次迭代会导致{foo: {bar: "baz"}}
,然后第二次遇到会把它修改为数组形式{foo: [{bar: "baz"}, {hello: "world"}]}
我会以不同的方式形成表示 XML 的对象;
Integer nodeType
String nodeName
String nodeValue
Array childNodes
Object attributes
现在,您可以拥有与子节点数量/等无关的相同形式
function nodeToObject(node) {
var obj = {}, i;
obj.nodeType = node.nodeType;
obj.nodeName = node.nodeName;
obj.nodeValue = node.nodeValue;
obj.childNodes = [];
obj.attributes = {};
if (node.childNodes && node.childNodes.length)
for (i = 0; i < node.childNodes.length; ++i)
obj.childNodes.push(nodeToObject(node.childNodes[i]));
if (node.attributes && node.attributes.length)
for (i = 0; i < node.attributes.length; ++i)
obj.attributes[node.attributes[i].nodeName] = node.attributes[i].nodeValue;
return obj;
}
然后将root_node
转换为 JSON,
JSON.stringify(nodeToObject(root_node));
在 JavaScript 中也可以朝相反的方向发展,使用一些基于 nodeType 的小逻辑来选择创建方法。
相关文章:
- DOM事件通过JSON转换为java
- 将日期时间从json转换为可读格式
- 如何将JSON转换为HTML下拉菜单
- 将JSON转换为放入Sdcard中的CSV文件
- 将JSON转换为typescript中的字符串数组
- 无法在Angular.js中将JSON转换为数组
- 使用jquery将json转换为数组
- 是否有将基于字符串的JSON转换为Mongoose Schema对象实例的本地功能
- 用D3js将JSON转换为SVG平面图
- 数组json转换为数组json
- 将JSON转换为特定的字符串格式
- 使用AJAX将JSON转换为PHP
- 使用JQuery将JSON转换为数组
- 使用 PHP 将 JSON 转换为 JavaScript
- 将 JavaScript 程序从 JSON 转换为 XML
- 将 JSON 转换为 CSV 格式
- 将 json 转换为 html 表的 JavaScript 不起作用
- 使用 D3 将分层 JSON 转换为 HTML
- 余烬原始 JSON 转换不起作用,但例外
- 如何将JSON转换为角度的HTML