About as simple as it should get:
<state name = "foo1>
<foobar item1="something1" item2="somethingelse1" item3="usefulitem1"/>
<foobar item1="something2" item2="somethingelse2" item3="usefulitem2"/>
<state name = "foo2">
...
root = lxml.etree.parse(fileName)
path = "./*[contains(text(),'useful')]"
someElement = root.xpath(path)
I'm trying to make a list of all the foobar's that have some item with the text 'useful' somewhere in it. I keep getting the error TypeError: 'lxml.etree._ElementTree' object is not iterable
I actually thought you would be able to XPath from a tree object. Huh. But you definitely can't iterate over it. To do that you need to get the root (element).
From the docs:
getroot(self)
Gets the root element for this tree.
So your code would look something like this instead.
<state name = "foo1>
<foobar item1="something1" item2="somethingelse1" item3="usefulitem1"/>
<foobar item1="something2" item2="somethingelse2" item3="usefulitem2"/>
<state name = "foo2">
...
tree = lxml.etree.parse(fileName)
root = tree.getroot()
path = "./*[contains(text(),'useful')]"
someElement = root.xpath(path)
The tricky thing is that when you use .fromstring instead of a file then you automatically have the root element, rather than an ElementTree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With