python如何使用libxml2获取属性值
我使用的是MINIDOM,但它不提供xpath方法 我现在尝试使用libxml2,但检索属性值时遇到问题 my xml的摘录如下所示:python如何使用libxml2获取属性值,python,xml,get,Python,Xml,Get,我使用的是MINIDOM,但它不提供xpath方法 我现在尝试使用libxml2,但检索属性值时遇到问题 my xml的摘录如下所示: <Class name="myclass1" version="0"> <Owner user-login="smagnoni"/> </Class> 返回: <Owner user-login="smagnoni"/> 我怎么才能得到“Smagoni”?手工解析字符串感觉工作过度。但是我在mini
<Class name="myclass1" version="0">
<Owner user-login="smagnoni"/>
</Class>
返回:
<Owner user-login="smagnoni"/>
我怎么才能得到“Smagoni”?手工解析字符串感觉工作过度。但是我在minidom中没有找到一个可以与.getAttribute(“属性名”)
相媲美的方法
有人能建议正确的方法或指导我使用文档吗?.prop('user-login')
应该可以:
import libxml2
import io
content='''\
<Class name="myclass1" version="0">
<Owner user-login="smagnoni"/>
</Class>
'''
doc = libxml2.parseMemory(content,len(content))
className='myclass1'
classVersion='0'
ris = doc.xpathEval('//Class[@name="'+className+'" and @version="'+classVersion+'"]/Owner')
elt=ris[0]
print(elt.prop('user-login'))
使用libxml2并提供更好的接口(),因此您可以充分利用libxml2的速度和xpath计算的所有好处
import lxml.etree as ET
doc = ET.parse(file)
owner = doc.find('/*/Class[@name="'+className+'" and @version="'+classVersion+'"]/Owner')
if owner:
print owner.get('user-login')
额外的好处是元素树api在python2.5中默认可用(尽管1.5中的版本不包括python2.7中添加的[@name='value']
xpath语法,但您可以在较旧的2.x版本的python中获得该语法)
您可以使用以下方法导入ElementTree api的任何兼容版本:
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
我在他们(似乎缺乏)的文档中也找不到这一点。它可能有一个.get方法。您也可以考虑使用“工业标准”LXML图书馆。LXML使用LIXML2并提供更好的接口(EntErthAPI)。ikanobori是正确的,不要使用默认的<代码> LBXML2< /CordyPython绑定,使用<代码> LXML<代码>,这是同一个库的一个更好的接口。br/>非常感谢!顺便说一句你有参考文档的链接吗??我真的在猜测如何使用这个图书馆。我找到了c实现的信息,但python的信息不多。即使它声称是一个稳定且非常好的库,我认为它缺乏文档。干杯ste@stefano:我不知道有什么特别好的文档——我主要使用lxml。我可以通过在IPython中摆弄代码来猜出答案——键入
ris。
显示ris的所有属性。这样闲逛让我找到了elt.prop
。很好。。。在本例中,使用.prop()更容易,但在另一种情况下,我可能会得到多个结果,您的foor循环可能会有所帮助。帮助触摸的Tnx。。。我在执行此操作时出错:“对于ris.properties中的属性:AttributeError:'list'对象没有属性'properties'”:(
for owner in ris:
for property in owner.properties:
if property.type == 'attribute':
print property.name
print property.content
import lxml.etree as ET
doc = ET.parse(file)
owner = doc.find('/*/Class[@name="'+className+'" and @version="'+classVersion+'"]/Owner')
if owner:
print owner.get('user-login')
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")