XML 到 JavaScript 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4200913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML to JavaScript Object
提问by Drew LeSueur
I am looking for a JavaScript library that parses an XML string and converts it to a JavaScript object. What are some good ones?
我正在寻找一个解析 XML 字符串并将其转换为 JavaScript 对象的 JavaScript 库。有哪些好的?
回答by Maylow Hayes
parses XML and returns a javascript object w/ a scheme that corresponds the xml. xml siblings w/ the same name are collapsed into arrays. nodes with names that can be found in the arrayTags parameter (array of tag name strings) always yeld arrays even in case of only one tag occurrence. arrayTags can be omitted. text nodes w/ only spaces are discarded. use console.log(parseXml(myxml)) to explore the output
解析 XML 并返回一个带有与 xml 对应的方案的 javascript 对象。具有相同名称的 xml 兄弟姐妹被折叠到数组中。具有可以在 arrayTags 参数(标记名称字符串数组)中找到的名称的节点总是 yeld 数组,即使只有一个标记出现也是如此。arrayTags 可以省略。仅丢弃空格的文本节点。使用 console.log(parseXml(myxml)) 探索输出
parseXml(xml, arrayTags)
{
var dom = null;
if (window.DOMParser)
{
dom = (new DOMParser()).parseFromString(xml, "text/xml");
}
else if (window.ActiveXObject)
{
dom = new ActiveXObject('Microsoft.XMLDOM');
dom.async = false;
if (!dom.loadXML(xml))
{
throw dom.parseError.reason + " " + dom.parseError.srcText;
}
}
else
{
throw "cannot parse xml string!";
}
function isArray(o)
{
return Object.prototype.toString.apply(o) === '[object Array]';
}
function parseNode(xmlNode, result)
{
if (xmlNode.nodeName == "#text") {
var v = xmlNode.nodeValue;
if (v.trim()) {
result['#text'] = v;
}
return;
}
var jsonNode = {};
var existing = result[xmlNode.nodeName];
if(existing)
{
if(!isArray(existing))
{
result[xmlNode.nodeName] = [existing, jsonNode];
}
else
{
result[xmlNode.nodeName].push(jsonNode);
}
}
else
{
if(arrayTags && arrayTags.indexOf(xmlNode.nodeName) != -1)
{
result[xmlNode.nodeName] = [jsonNode];
}
else
{
result[xmlNode.nodeName] = jsonNode;
}
}
if(xmlNode.attributes)
{
var length = xmlNode.attributes.length;
for(var i = 0; i < length; i++)
{
var attribute = xmlNode.attributes[i];
jsonNode[attribute.nodeName] = attribute.nodeValue;
}
}
var length = xmlNode.childNodes.length;
for(var i = 0; i < length; i++)
{
parseNode(xmlNode.childNodes[i], jsonNode);
}
}
var result = {};
for (let i = 0; i < dom.childNodes.length; i++)
{
parseNode(dom.childNodes[i], result);
}
return result;
}
回答by icyrock.com
Here's a nice xml2json and json2xml converter:
这是一个不错的 xml2json 和 json2xml 转换器:
- http://goessner.net/download/prj/jsonxml/
- Related tutorial: http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html
Here's another one:
这是另一个:
Depending on your needs, you might be able to use a standard parser (see http://www.w3schools.com/XML/tryit.asp?filename=tryxml_parsertest2) and xpath (http://www.w3schools.com/xpath/default.asp) - here's an example:
根据您的需要,您可以使用标准解析器(请参阅http://www.w3schools.com/XML/tryit.asp?filename=tryxml_parsertest2)和 xpath(http://www.w3schools.com/xpath /default.asp) - 这是一个例子:
and a few nice tutorials:
和一些不错的教程:
回答by Tom G
The xml2json javascript file from https://bitbucket.org/surenrao/xml2jsonis all you need to do this.
来自https://bitbucket.org/surenrao/xml2json的 xml2json javascript 文件是您执行此操作所需的全部内容。
Here's the download link for quick download: https://bitbucket.org/surenrao/xml2json/get/0e0989dfe48e.zip
这是快速下载的下载链接:https: //bitbucket.org/surenrao/xml2json/get/0e0989dfe48e.zip
Once included in your project, here's some sample code to get you started:
一旦包含在您的项目中,这里有一些示例代码可以帮助您入门:
var xmlStr = "<root><person><name>Bob Dylan</name></person></root>";
var jsObj = X2J.parseXml(xmlStr);
var result = jsObj[0].root[0].person[0].name[0].jValue; //Bob Dylan