如何使用 d3.js 导入 XML 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10682856/
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
How to import XML data using d3.js?
提问by Olga
can someone provide a basic example how to import data from an XML file using d3?
有人可以提供一个基本示例,如何使用 d3 从 XML 文件导入数据?
My XML file looks like this:
我的 XML 文件如下所示:
<data>
<value>71</value>
<value>12</value>
<value>44</value>
<value>88</value>
</data>
How can I add these values to a data array? Here is what I tried so far:
如何将这些值添加到数据数组中?这是我到目前为止尝试过的:
d3.xml("values.xml", function(xml) {
d3.select(xml).selectAll("data").each(function(data) {
d3.select(data).selectAll("value");
//add data to array?;
};
});
//use Array
回答by Lars Grammel
The XML object that is passed into the callback is the root element of the XML DOM (see https://github.com/mbostock/d3/wiki/Requests#wiki-d3_xml), and therefore you need to process it using the JavaScript XML/DOM access facilities.
传入回调的 XML 对象是 XML DOM 的根元素(请参阅https://github.com/mbostock/d3/wiki/Requests#wiki-d3_xml),因此您需要使用 JavaScript 处理它XML/DOM 访问设施。
I have written a small example that shows how to use d3.xml to create a bar chart (based on the original d3 barchart example http://mbostock.github.com/d3/tutorial/bar-1.html):
我写了一个小例子,展示了如何使用 d3.xml 创建条形图(基于原始的 d3 barchart 示例http://mbostock.github.com/d3/tutorial/bar-1.html):
Link to see example: http://bl.ocks.org/2772585
链接查看示例:http: //bl.ocks.org/2772585
Link with XML code: https://gist.github.com/lgrammel/2772585
与 XML 代码链接:https: //gist.github.com/lgrammel/2772585

