如何使用python将XML转换为JSON?
我有下面的XML,并保存在名为movies.XML的文件中。我只需要转换成带有一些值的JSON。对于直接转换,我可以使用xmltodict。我正在使用etree和etree.XMLParser()。在这之后,我正在努力进行弹性搜索。我已经使用attrib方法成功地提取了单个节点如何使用python将XML转换为JSON?,python,xml,beautifulsoup,lxml,Python,Xml,Beautifulsoup,Lxml,我有下面的XML,并保存在名为movies.XML的文件中。我只需要转换成带有一些值的JSON。对于直接转换,我可以使用xmltodict。我正在使用etree和etree.XMLParser()。在这之后,我正在努力进行弹性搜索。我已经使用attrib方法成功地提取了单个节点 <?xml version="1.0" encoding="UTF-8" ?> <collection> <genre category="Action">
<?xml version="1.0" encoding="UTF-8" ?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the
Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must
oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>
<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie about a funny guy</description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>
</collection>
我已经从datacamp完成了基本操作,无法获得所需的输出
from lxml import etree
parser = etree.XMLParser()
tree= etree.parse('movies.xml', parser)
data= tree.find("genre[@category='Action']")
json= {}
for child in enumerate(data.getchildren()):
temp = {}
for content in child[1].getchildren():
temp[content.attrib.get('title')] = content.text.strip()
json[child[0]] = temp.keys()
json
我建议使用XSLT将XML转换为JSON:
导入json
从lxml导入etree
XSL=''
{
}
"
": [
"
"
,
]
,
'''
#负载输入
dom=etree.parse('movies.xml')
#加载XSLT
transform=etree.XSLT(etree.fromstring(XSL))
#在加载的dom上应用XSLT
json_text=str(转换(dom))
#json_文本包含转换为json格式的数据。
#您可以将其与JSON API一起使用。例子:
data=json.load(json_文本)
打印(数据)
输出:
{'Action': ['Indiana Jones: The raiders of the lost Ark', 'THE KARATE KID', 'Back 2 the Future', 'X-Men', 'Batman Returns', 'Reservoir Dogs'], 'Thriller': ['ALIEN', "Ferris Bueller's Day Off", 'American Psycho']}
但是,我不明白“第二输出”和“第三输出”想要实现什么,因为这些输出似乎是常量。我建议使用XSLT将XML转换为JSON:
导入json
从lxml导入etree
XSL=''
{
}
"
": [
"
"
,
]
,
'''
#负载输入
dom=etree.parse('movies.xml')
#加载XSLT
transform=etree.XSLT(etree.fromstring(XSL))
#在加载的dom上应用XSLT
json_text=str(转换(dom))
#json_文本包含转换为json格式的数据。
#您可以将其与JSON API一起使用。例子:
data=json.load(json_文本)
打印(数据)
输出:
{'Action': ['Indiana Jones: The raiders of the lost Ark', 'THE KARATE KID', 'Back 2 the Future', 'X-Men', 'Batman Returns', 'Reservoir Dogs'], 'Thriller': ['ALIEN', "Ferris Bueller's Day Off", 'American Psycho']}
我不明白你想通过“第二次输出”和“第三次输出”实现什么,因为这些输出似乎是常量。问题出在哪里?请给我们看看你迄今为止写的代码。Thriller?X战警?有不同的标签。请再次检查您的预期输出。预期输出已纠正请查看问题所在?请给我们看看你迄今为止写的代码。Thriller?X战警?有不同的标签。再次检查您的expect输出。expected out已更正请看一看它是否完美工作,Python编码是否值得欣赏:)它是否完美工作,Python编码是否值得欣赏:)