对于最近提出的问题,我开始怀疑是否有一种非常简单的方法来处理Python中的XML文档.一种pythonic方式,如果你愿意的话.
如果我举一个例子,也许我可以解释得最好:让我们说以下 - 我认为这是一个很好的例子,说明如何(错误地)在Web服务中使用XML - 是我从http请求到http://www.google的回复的.com/IG/API?天气= 94043
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
<forecast_information>
<city data="Mountain View, CA"/>
<postal_code data="94043"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2010-06-23"/>
<current_date_time data="2010-06-24 00:02:54 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Sunny"/>
<temp_f data="68"/>
<temp_c data="20"/>
<humidity data="Humidity: 61%"/>
<icon data="/ig/images/weather/sunny.gif"/>
<wind_condition data="Wind: NW at 19 mph"/>
</current_conditions>
...
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="59"/>
<high data="75"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<condition data="Partly Cloudy"/>
</forecast_conditions>
</weather>
</xml_api_reply>
Run Code Online (Sandbox Code Playgroud)
在加载/解析这样的文档之后,我希望能够像访问那样简单地访问信息
>>> xml['xml_api_reply']['weather']['forecast_information']['city'].data
'Mountain View, CA'
Run Code Online (Sandbox Code Playgroud)
要么
>>> xml.xml_api_reply.weather.current_conditions.temp_f['data']
'68'
Run Code Online (Sandbox Code Playgroud)
从我到目前为止看到的情况来看,这似乎ElementTree与我的梦想最接近.但它并不存在,在使用XML时仍然有一些笨手笨脚的事情要做.OTOH,我在想的并不是那么复杂 - 可能只是解析器顶部的薄单板 - 但它可以减少处理XML的烦恼.有这么神奇吗?(如果没有 - 为什么?)
PS.注意我已经尝试过BeautifulSoup了,虽然我喜欢它的方法,但它有空<element/>s的实际问题- 请参阅下面的注释中的示例.
Rya*_*rom 15
lxml已被提及.您也可以查看lxml.objectify进行一些非常简单的操作.
>>> from lxml import objectify
>>> tree = objectify.fromstring(your_xml)
>>> tree.weather.attrib["module_id"]
'0'
>>> tree.weather.forecast_information.city.attrib["data"]
'Mountain View, CA'
>>> tree.weather.forecast_information.postal_code.attrib["data"]
'94043'
Run Code Online (Sandbox Code Playgroud)
你想要一个薄的贴面?这很容易做饭.尝试使用ElementTree的以下简单包装作为开始:
# geetree.py
import xml.etree.ElementTree as ET
class GeeElem(object):
"""Wrapper around an ElementTree element. a['foo'] gets the
attribute foo, a.foo gets the first subelement foo."""
def __init__(self, elem):
self.etElem = elem
def __getitem__(self, name):
res = self._getattr(name)
if res is None:
raise AttributeError, "No attribute named '%s'" % name
return res
def __getattr__(self, name):
res = self._getelem(name)
if res is None:
raise IndexError, "No element named '%s'" % name
return res
def _getelem(self, name):
res = self.etElem.find(name)
if res is None:
return None
return GeeElem(res)
def _getattr(self, name):
return self.etElem.get(name)
class GeeTree(object):
"Wrapper around an ElementTree."
def __init__(self, fname):
self.doc = ET.parse(fname)
def __getattr__(self, name):
if self.doc.getroot().tag != name:
raise IndexError, "No element named '%s'" % name
return GeeElem(self.doc.getroot())
def getroot(self):
return self.doc.getroot()
Run Code Online (Sandbox Code Playgroud)
你调用它:
>>> import geetree
>>> t = geetree.GeeTree('foo.xml')
>>> t.xml_api_reply.weather.forecast_information.city['data']
'Mountain View, CA'
>>> t.xml_api_reply.weather.current_conditions.temp_f['data']
'68'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15532 次 |
| 最近记录: |