Using XPATH in python etree to select node with out a specific attribute

﹥>﹥吖頭↗ 提交于 2019-12-01 21:47:33

ElementTree only supports a subset of XPath 1.0. See https://docs.python.org/2/library/xml.etree.elementtree.html#xpath-support. Functions such as not() or count() do not work.

Here is how you can select neighbor elements that don't have a direction attribute without using XPath:

tree = ET.parse(fileName)

for n in tree.iter('neighbor'):  
    if not(n.get('direction')):  # If the element has no 'direction' attribute...
        print n.get('name')      # then print the value of the 'name' attribute
shahzeb haider
import xml.etree.ElementTree as etree

xmlD = etree.parse('xmlTest.xml')
root = xmlD.getroot()

for child in root:
    for children in child:
        print(children.text)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!