将Suds XML转换为字典

时间:2019-09-12 16:35:11

标签: python xml suds

先谢谢了。我正在尝试将suds的输出转换为python字典。我尝试过的各种软件包似乎只包含父节点,却不考虑子节点。

以下是示例回复:

<?xml version="1.0" encoding="utf-16"?>
<Records count="81">
    <Metadata>
        <FieldDefinitions>
            <FieldDefinition id="22100" guid="123" name="A" alias="A" />
            <FieldDefinition id="22120" guid="234" name="B" alias="B" />
        </FieldDefinitions>
    </Metadata>
<LevelCounts></LevelCounts>
    <LevelCount id="332" guid="345" count="81" />
</LevelCounts>

<Record contentId="251906" levelId="332" levelGuid="123" moduleId="534" parentId="0">
    <Field id="22156" guid="234" type="6">251906</Field>
    <Field id="22133" guid="234" type="4"><ListValues><ListValue id="80897" displayName="Closed">Closed</ListValue></ListValues></Field>
</Record>

这甚至都不会被解析,但是即使我只记录,我也只会得到GUID,ID和类型。

xml_records = """
<Record contentId="251906" levelId="332" levelGuid="123" moduleId="534" parentId="0">
    <Field id="22156" guid="234" type="6">251906</Field>
    <Field id="22133" guid="234" type="4"><ListValues><ListValue id="80897" displayName="Closed">Closed</ListValue></ListValues></Field>
</Record>
"""


root = ET.fromstring(xml_records.encode('utf-16'))

li = []
for c in root:
    li.append(c.attrib)

df = pd.DataFrame(li)
df.head()


guid    id  type
234 22156   6
234 22133   4

有没有一种方法可以将xml输出到表中而无需手动解析文本?

0 个答案:

没有答案