我只想解析一个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街区,我不知道为什么?你知道吗
检查
name is 'Developments'
总是返回false
,因为child.tag
返回的是{xmlns}tagname
格式的值。你知道吗对于您的情况:
你可以参考这个question的答案。你知道吗
简单的字符串方法
strip()
、find()
、split()
或re
可以帮助您跳过命名空间进行比较。你知道吗Python相关文档:https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
相关问题 更多 >
编程相关推荐