使用etree解析xml时无法返回子值

问题描述 投票:0回答:1

我有一个看起来像的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<FullReport
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <firm>1426</firm>
    <reportDate>07FEB2020_18:00:00.000000</reportDate>
    <rooms>
        <room>
            <roomID>PCHAT-0x0000000000000637</roomID>
            <roomTitle>FX - WBB - CTON</roomTitle>
            <description>global chat</description>
            <creationDate></creationDate>
            <removalDate></removalDate>
            <lastActivityDate>02/07/2020 12:26:24</lastActivityDate>
            <status>Active</status>
            <membership>Bilateral</membership>
            <isAnonymous>false</isAnonymous>
            <hasActiveAdmins>true</hasActiveAdmins>
            <activeUserCount>17</activeUserCount>
            <distinctFirmsInRoom>2</distinctFirmsInRoom>
            <isInternalOnly>false</isInternalOnly>
            <isIncognitoForum>false</isIncognitoForum>
        </room>
        <users>
            <uuid>6820</uuid>
            <bbgEmail>[email protected]</bbgEmail>
            <fullName>SEAN JONES</fullName>
            <firmName>BANK OF TEST</firmName>
            <firmNumber>1400</firmNumber>
            <accountNumber>51067</accountNumber>
            <accountName>BANK OF TEST</accountName>
            <inviteDate>01/07/2013 22:00:39</inviteDate>
            <isDeleted>false</isDeleted>
            <isAdmin>false</isAdmin>
            <isCreator>false</isCreator>
            <roomAlias>CTON</roomAlias>
            <corpEmail>[email protected]</corpEmail>
            <city>LONDON</city>
        </users>
        <users>
            <uuid>6820</uuid>
            <bbgEmail>[email protected]</bbgEmail>
            <fullName>SEAN SMITH</fullName>
            <firmName>BANK OF TEST</firmName>
            <firmNumber>1400</firmNumber>
            <accountNumber>51067</accountNumber>
            <accountName>BANK OF TEST</accountName>
            <inviteDate>01/07/2013 22:00:39</inviteDate>
            <isDeleted>false</isDeleted>
            <isAdmin>false</isAdmin>
            <isCreator>false</isCreator>
            <roomAlias>CTON</roomAlias>
            <corpEmail>[email protected]</corpEmail>
            <city>LONDON</city>
        </users>
</FullReport>
</FullReport>

我正在使用以下代码解析数据:

import xml.etree.cElementTree as et

tree = et.parse('test.xml')
print (tree.getroot())
root = tree.getroot()
print ("tag=%s, attrib=%s" % (root.tag, root.attrib))

for child in root:
        print (child.tag, child.attrib)
        if child.tag == "room":
            for step_child in child:
                print (step_child.tag)

# get the information via the children!
print ("-" * 40)
print ("Iterating using a getchildren()")
print ("-" * 40)
rooms = root.getchildren()
for room in rooms:
    room_children = room.getchildren()
    for room_child in room_children:
        print ("%s=%s" % (room_child.tag, room_child.text))

当我打印room_child.tag和room_child.text时,我看不到值吗?这很新,所以我不确定我缺少什么?

返回的结果是:

enter image description here

我的最终目标是遍历每个值并转换为CSV,但是我一直无法访问这些值,并且不确定为什么没有数据返回。我希望最终的csv看起来类似于csv中每个用户的一行]

enter image description here

python xml elementtree
1个回答
1
投票

当您运行for child in root:时,此循环将迭代[[only在FullReport

的直接后代中,在您的情况下:firm
© www.soinside.com 2019 - 2024. All rights reserved.