元素树搜索帮助(python、xml)
我正在尝试查找其属性元素树搜索帮助(python、xml),python,xml,Python,Xml,我正在尝试查找其属性class为'TRX'且其属性distName以'PLMN-PLMN/BSC-208812/BCF-1/BTS-1'开头的所有子元素 这样的事情可能吗 root[0].findall("*[@class='TRX'][@distName='PLMN-PLMN/BSC-208812/BCF-1/BTS-1*']") 我使用cElementTree而不是lxml,因为它在我的comp上要快得多。ElementTree实际上可以做到这一点 您可能需要使用/*而不是* Python
class
为'TRX'且其属性distName
以'PLMN-PLMN/BSC-208812/BCF-1/BTS-1'开头的所有子元素
这样的事情可能吗
root[0].findall("*[@class='TRX'][@distName='PLMN-PLMN/BSC-208812/BCF-1/BTS-1*']")
我使用cElementTree而不是lxml,因为它在我的comp上要快得多。ElementTree实际上可以做到这一点 您可能需要使用
/*
而不是*
Python2.7附带的1.3.0版具有您想要的XPath功能
示例
from xml.etree import ElementTree
et = ElementTree.fromstring("""
<r>
<b>
<a class='c' foo='e'>this is e</a>
<a class='c' foo='f'>this is f</a>
<a class='c' foo='g'>this is g</a>
</b>
</r>
""")
if __name__ == '__main__':
print ElementTree.VERSION
print "* c and f", et.findall("*[@class='c'][@foo='f']")
print ".//* c and f", et.findall(".//*[@class='c'][@foo='f']")
print ".//* c", et.findall(".//*[@class='c']")
print ".//* f", et.findall(".//*[@foo='f']")
从xml.etree导入元素树
et=ElementTree.fromstring(“”)
这是e
这是f
这是g
""")
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
print ElementTree.VERSION
打印“*c和f”,et.findall(“*[@class='c'][@foo='f']”)
打印“//*c和f”,et.findall(“./*[@class='c'][@foo='f']”)
打印“//*c”,et.findall(“./*[@class='c']”)
打印“//*f”,et.findall(“./*[@foo='f']”)
输出
1.3.0
* c and f []
.//* c and f [<Element 'a' at 0x10049be50>]
.//* c [<Element 'a' at 0x10049bdd0>, <Element 'a' at 0x10049be50>, <Element 'a' at 0x10049bf10>]
.//* f [<Element 'a' at 0x10049be50>]
1.3.0
*c和f[]
.//*c和f[]
.//*c[,]
.//*f[]
我想我的问题表述错了,我想找到所有class='c',它们的foo属性都以my bad开头,我没有读过。显示它不支持starts-with(),这正是您需要的。。。由于xml的结构,它仍然比lxml快。。。