如何使用python将xml文件转换为JSON? [英] How can i convert an xml file into JSON using python?

查看:638
本文介绍了如何使用python将xml文件转换为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,想要使用python转换为JSON文件,但是它的nt对我有用.

I have an XML file which I want to convert into JSON file using python, but its nt working out for me.

<?xml version="1.0"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

我正在使用ElementTree解析上述XML文件,并将其提供给Simplejson进行序列化:

The above XML file I am parsing using ElementTree and giving it to Simplejson to serialize like this:

from xml.etree import ElementTree as ET
import simplejson

tree = ET.parse(Xml_file_path)
simplejson.dumps(tree)

它给我一个错误:TypeError:位于0x00C49DD0的xml.etree.ElementTree.ElementTree对象不是JSON可序列化的.

It gives me an error: TypeError: xml.etree.ElementTree.ElementTree object at 0x00C49DD0 is not JSON serializable.

推荐答案

这可能是您正在寻找的东西:

This is probably what you are looking for:

https://github.com/mutaku/xml2json

import xml2json

s = '''<?xml version="1.0"?>
<note>
   <to>Tove</to>
   <from>Jani</from>
   <heading>Reminder</heading>
   <body>Don't forget me this weekend!</body>
</note>'''
print xml2json.xml2json(s)

这篇关于如何使用python将xml文件转换为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆