【问题标题】:python - problem converting xml data to jsonpython - 将xml数据转换为json的问题
【发布时间】:2020-08-06 01:36:20
【问题描述】:

这是我试图转换成 json 数据的 xml 数据和代码:

import xmltodict
import json

xml = '''<?xml version="1.0" encoding="UTF-8" ?>
<XML xmlns:r="#SchemaA2A">
<Schema name="SchemaA2A"  xmlns="urn:schemas-microsoft-com:xml-data"  xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="ROOT" content="eltOnly" model="closed" order="many">
<AttributeType name="Response" dt:type="i4"/>
<AttributeType name="ErrorNumber" dt:type="i4"/>
<AttributeType name="ErrorDescription" dt:type="string"/>
<AttributeType name="ErrorDisplay" dt:type="boolean"/>
<attribute type="Response"/>
<attribute type="ErrorNumber"/>
<attribute type="ErrorDescription"/>
<attribute type="ErrorDisplay"/>
</ElementType>
<ElementType name="ROW" content="empty" model="closed">
<AttributeType name="Account #" dt:type="string"/>
<AttributeType name="Tran Date" dt:type="string"/>
<AttributeType name="Post Date" dt:type="string"/>
<AttributeType name="Description" dt:type="string"/>
<AttributeType name="Reference #" dt:type="string"/>
<AttributeType name="Amt" dt:type="string"/>
<attribute type="Account #"/>
<attribute type="Tran Date"/>
<attribute type="Post Date"/>
<attribute type="Description"/>
<attribute type="Reference #"/>
<attribute type="Amt"/>
</ElementType>
</Schema>
<r:ROOT Response="1" ErrorNumber="0" ErrorDescription="" ErrorDisplay="1" >
<r:ROW Account #="xxxxxxxxxx055819" Tran Date="04/09/2020" Post Date="04/09/2020" Description="ValueLoad" Reference #="" Amt="50" />
<r:ROW Account #="xxxxxxxxxx055819" Tran Date="04/22/2020" Post Date="04/22/2020" Description="Balance Adjustment Debit Cardholder" Reference #="" Amt="-10" />
</r:ROOT>
</XML>
'''

mydict=xmltodict.parse(xml, encoding="utf-8")
json_data=json.dumps(mydict)
print(json_data)

它正在返回一个错误:

xml.parsers.expat.ExpatError: not well-formed (invalid token): line 30, column 15

我认为这是指向结束的地方

真的很困惑为什么我会收到这个错误。任何帮助将不胜感激。

【问题讨论】:

    标签: python json xml type-conversion


    【解决方案1】:

    您的 XML 格式不正确。 XML 元素属性中不能有空格或 # 符号。

    【讨论】:

    • 你是对的,@python_help 使用XML Formatter 看看你的错误在哪里
    猜你喜欢
    • 2013-03-09
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2019-05-31
    相关资源
    最近更新 更多