将 XML 解析为 JSON
我有一个 XML 文件,例如
<stock><name>AXL</name><time>19-07</time><price>11.34</price></stock>
<stock><name>AIK</name><time>19-07</time><price>13.54</price></stock>
<stock><name>ALO</name><time>19-07</time><price>16.32</price></stock>
<stock><name>APO</name><time>19-07</time><price>13.56</price></stock>
...............more
如何将其解析为 JSON 结构文件?
I have an XML file, like
<stock><name>AXL</name><time>19-07</time><price>11.34</price></stock>
<stock><name>AIK</name><time>19-07</time><price>13.54</price></stock>
<stock><name>ALO</name><time>19-07</time><price>16.32</price></stock>
<stock><name>APO</name><time>19-07</time><price>13.56</price></stock>
...............more
How can I parse this into JSON structure file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于简单的解决方案,我推荐 Jackson,这是一个用于生成和读取带有 XML 扩展的 JSON 的 Java 库,因为它可以转换任意复杂的 XML只需几行简单的代码即可将其转换为 JSON。
input.xml
Java 代码:
此演示使用 Jackson 1.7.7 (较新的 1.7.8 也应该可以工作),Jackson XML Databind 0.5.3(尚不兼容)杰克逊1.8) 和 Stax2 3.1.1。
For a simple solution, I recommend Jackson, a Java library for generating and reading JSON with an extension for XML, as it can transform arbitrarily complex XML into JSON with just a few simple lines of code.
input.xml
The Java Code:
This demo uses Jackson 1.7.7 (the newer 1.7.8 should also work), Jackson XML Databind 0.5.3 (not yet compatible with Jackson 1.8), and Stax2 3.1.1.
http://keithchadwick。 wordpress.com/2009/03/14/converting-xml-to-json-with-xsl-part-2/
您尚未指定语言...所以...我还没有比认为“您已经有了 XML,可能您可以访问 xsl/xslt”更具体:
http ://www.w3.org/TR/xslt
http://www.thomasfrank.se/xml_to_json.html
http://keithchadwick.wordpress.com/2009/03/14/converting-xml-to-json-with-xsl-part-2/
You haven't specified language... so... I haven't got more specific on it than to think "you already have XML, probably you have access to xsl/xslt":
http://www.w3.org/TR/xslt
http://www.thomasfrank.se/xml_to_json.html