使用ElementTree examp在Python中解析XML

2024-04-18 13:34:13 发布

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

我很难找到一个好的、基本的例子来说明如何使用元素树在python中解析XML。据我所知,这似乎是解析XML时最容易使用的库。下面是我正在使用的XML示例:

<timeSeriesResponse>
    <queryInfo>
        <locationParam>01474500</locationParam>
        <variableParam>99988</variableParam>
        <timeParam>
            <beginDateTime>2009-09-24T15:15:55.271</beginDateTime>
            <endDateTime>2009-11-23T15:15:55.271</endDateTime>
        </timeParam>
     </queryInfo>
     <timeSeries name="NWIS Time Series Instantaneous Values">
         <values count="2876">
            <value dateTime="2009-09-24T15:30:00.000-04:00" qualifiers="P">550</value>
            <value dateTime="2009-09-24T16:00:00.000-04:00" qualifiers="P">419</value>
            <value dateTime="2009-09-24T16:30:00.000-04:00" qualifiers="P">370</value>
            .....
         </values>
     </timeSeries>
</timeSeriesResponse>

我可以做我需要的,使用硬编码的方法。但我需要我的代码更具动态性。以下是有效的方法:

tree = ET.parse(sample.xml)
doc = tree.getroot()

timeseries =  doc[1]
values = timeseries[2]

print child.attrib['dateTime'], child.text
#prints 2009-09-24T15:30:00.000-04:00, 550

以下是我尝试过的一些事情,没有一个成功,报告说他们找不到timeSeries(或者我尝试过的其他任何东西):

tree = ET.parse(sample.xml)
tree.find('timeSeries')

tree = ET.parse(sample.xml)
doc = tree.getroot()
doc.find('timeSeries')

基本上,我想加载xml文件,搜索timeSeries标记,并遍历值标记,返回日期时间和标记本身的值;我在上面的示例中所做的一切,但不是硬编码我感兴趣的xml部分。有谁能给我举几个例子,或者给我一些建议,告诉我如何解决这个问题?


谢谢你的帮助。使用下面的两个建议对我提供的示例文件都有效,但是,它们对完整文件无效。下面是我使用Ed Carrel方法时从实际文件中得到的错误:

 (<type 'exceptions.AttributeError'>, AttributeError("'NoneType' object has no attribute 'attrib'",), <traceback object at 0x011EFB70>)

我发现在真实的文件中有一些不喜欢的东西,所以我逐渐删除,直到它起作用。以下是我更改的行:

originally: <timeSeriesResponse xsi:schemaLocation="a URL I removed" xmlns="a URL I removed" xmlns:xsi="a URL I removed">
 changed to: <timeSeriesResponse>

 originally:  <sourceInfo xsi:type="SiteInfoType">
 changed to: <sourceInfo>

 originally: <geogLocation xsi:type="LatLonPointType" srs="EPSG:4326">
 changed to: <geogLocation>

删除具有“xsi:…”的属性修复了该问题。“xsi:…”是无效的XML吗?我很难用编程的方式删除这些。有建议的解决办法吗?

这是完整的XML文件:http://www.sendspace.com/file/lofcpt


当我最初问这个问题时,我不知道XML中的名称空间。现在我知道发生了什么,不必删除“xsi”属性,这些属性是命名空间声明。我只是将它们包含在xpath搜索中。有关lxml中名称空间的更多信息,请参见this page


Tags: 文件方法tree示例datetimedocvaluexml
2条回答

如果我正确理解你的问题:

for elem in doc.findall('timeSeries/values/value'):
    print elem.get('dateTime'), elem.text

或者如果您愿意(并且只有一次出现timeSeries/values

values = doc.find('timeSeries/values')
for value in values:
    print value.get('dateTime'), elem.text

findall()方法返回所有匹配元素的列表,而find()只返回第一个匹配元素。第一个示例循环所有找到的元素,第二个循环循环values元素的子元素,在本例中会导致相同的结果。

但是,我不明白找不到timeSeries的问题来自何处。也许你忘了打电话了?(请注意,如果您将路径表达式更改为例如/timeSeriesResponse/timeSeries/values//timeSeries/values,则实际上并不需要它,因为您也可以从elementtree本身开始工作)

因此,现在我的框中有ElementTree 1.2.6,并对您发布的XML块运行以下代码:

import elementtree.ElementTree as ET

tree = ET.parse("test.xml")
doc = tree.getroot()
thingy = doc.find('timeSeries')

print thingy.attrib

得到了以下信息:

{'name': 'NWIS Time Series Instantaneous Values'}

它似乎在不需要使用数字索引的情况下找到了timeSeries元素。

现在有用的是知道当你说“它不起作用”时你的意思,因为它对我同样的输入起作用,所以不太可能以某种明显的方式破坏ElementTree。使用任何错误消息、回溯或您可以提供的任何帮助来更新您的问题。

相关问题 更多 >