【发布时间】:2013-09-23 07:15:42
【问题描述】:
XML 文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MINiML
xmlns="http://www.ncbi.nlm.nih.gov/geo/info/MINiML"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ncbi.nlm.nih.gov/geo/info/MINiML http://www.ncbi.nlm.nih.gov/geo/info/MINiML.xsd"
version="0.5.0" >
<Contributor iid="contrib1">
<Person><First>ENCODE</First><Last>DCC</Last></Person>
<Email>encode-help@lists.stanford.edu</Email>
<Organization>ENCODE DCC</Organization>
<Address>
<Line>300 Pasteur Dr</Line>
<City>Stanford</City>
<State>CA</State>
<Zip-Code>94305-5120</Zip-Code>
<Country>USA</Country>
</Address>
</Contributor>
</MINiML>
这是我在 Python 中使用 ElementTree 的方法:
import xml.etree.cElementTree as ET
tree=ET.parse("the_file_above.xml")
root = tree.getroot()
for c in root:
print c.tag, c.attrib
返回:
{http://www.ncbi.nlm.nih.gov/geo/info/MINiML}Contributor {'iid': 'contrib1'}
而c.tag 的值是'{http://www.ncbi.nlm.nih.gov/geo/info/MINiML}Contributor',我预计是Contributor。我不确定标签中的长网址是如何混合的。有人对此有想法吗?
【问题讨论】:
-
这是这个标签来自的 XML 命名空间。我确信库也有办法获取本地标签名称。
标签: python html xml tags elementtree