![](/img/trans.png)
[英]python, lxml or etree to get a parent of a node containing some text
[英]Python lxml etree check if node exists
我有这个XML:
<MasterPage>
<NextPage>
<prefix>
我想检查prefix
节点是否存在; 我试过了,但是没有用:
self.doc=etree.parse(xmlFile)
if hasattr(self.doc, 'MasterPage/NextPage/prefix'):
>>> from lxml.html import fromstring
>>> import lxml.html as PARSER
>>> data = """<MasterPage>
... <NextPage>
... <prefix>"""
>>> root = PARSER.fromstring(data)
>>> node_list = []
>>> for ele in root.getiterator():
... node_list.append(ele.tag)
...
>>> if "prefix" in node_list:
... print "True"
...
True
>>> node_list
['masterpage', 'nextpage', 'prefix']
您可以使用etree库中的find()或findall()函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.