在python中解析一个xml文件,其中嵌套的标记是

2024-10-03 19:27:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想解析一个xml文件,就像

 <?xml version="1.0" encoding="UTF-8"?><Significant Major="3" Minor="0" Revision="1" xmlns="urn:reuterscompanycontent:significantdevelopments03"><RepNo>0091N</RepNo><CompanyName Type="Primary">XYZ</CompanyName><Production Date="2017-02-23T18:10:39" /><Developments><Development ID="3534388"><Dates><Source>2017-02-23T18:18:32</Source><Initiation>2017-02-23T18:18:32</Initiation><LastUpdate>2017-02-23T18:23:26</LastUpdate></Dates><Flags><FrontPage>0</FrontPage><Significance>1</Significance></Flags><Topics><Topic1 Code="254">Regulatory / Company Investigation</Topic1></Topics><Headline>FTC approves final order settling charges for Abbott's deal with St. Jude Medical</Headline></Development></Developments></Significant>

我只想解析Development标记并解析它的每个嵌套标记 我有以下代码:

import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='../rawdata/SigDev_0091N.xml')

#get the root element
root = tree.getroot()

#print root.tag, root.attrib

for child in root:
#print child.tag, child.attrib
    name = child.tag
    print name
    print 'at line 13'
    if name is 'Developments':
        print 'at line 15'
        for devChild in name['Developments']:
            print devChild.tag,devChild.attrib

它不会进入if街区,我不知道为什么?你知道吗


Tags: namechildfortagrootxmldevelopmentdates
1条回答
网友
1楼 · 发布于 2024-10-03 19:27:31

检查name is 'Developments'总是返回false,因为child.tag返回的是{xmlns}tagname格式的值。你知道吗

对于您的情况:

name = {urn:reuterscompanycontent:significantdevelopments03}Developments

你可以参考这个question的答案。你知道吗

简单的字符串方法strip()find()split()re可以帮助您跳过命名空间进行比较。你知道吗

Python相关文档:https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml-with-namespaces

相关问题 更多 >