The find method can accept some limited Xpath expressions, you can use this to extract only IPs which are marked as Primary:
from xml.etree import ElementTree
tree = ElementTree.fromstring(sample)
for node in tree.iter('Host'):
hostname = node.find('Name').text
ips = node.findall("Networking[Primary='Yes']/IP")
print hostname
for ip in ips:
print ip.text
For further information on what XPath expressions are allowed see the documentation at: https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element
The sample XML provided in the question is malformed in a couple of areas (presumably when it was obfuscated for posting, or the code example given could never have worked). The Type tag is closed twice, and the Primary tags are mismatched with closing Weight tags
Answer from James on Stack Overflowuse xpath:
doc.xpath('.//item[timeleft/text()!="PT0S"]/title/text()')
You can use Python's ElementTree API and simple list comprehension:
import xml.etree.ElementTree as ET
tree = ET.parse('your_xml_file.xml')
root = tree.getroot()
titles = [item.find('Title').text for item in root.findall('Item') if item.find('TimeLeft').text != 'PT0S']
titles will be a list of titles of items for which TimeLeft is not PT0S. This is easier to read than an XPath-based solution (if you're not familiar with XPath) in my opinion.
Use ElementTree lib to pull out the child nodes. This might help you.
import xml.etree.ElementTree as ET
tree = ET.parse("file.xml")
root = tree.getroot()
for child in root:
print({x.tag for x in root.findall(child.tag+"/*")})
The solution using xml.etree.ElementTree module:
import xml.etree.ElementTree as ET
tree = ET.parse("yourxml.xml")
root = tree.getroot()
tag_names = {t.tag for t in root.findall('.//country/*')}
print(tag_names) # print a set of unique tag names
The output:
{'gdp', 'rank', 'neighbor', 'year'}
'.//country/*'- xpath expression to extract all child elements of nodecountry
here is simple example for iterating from head to tail with a recursive method and cElementTree(15-20x faster), you can than collect the needed information from that
import xml.etree.cElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
def get_tail(root):
for child in root:
print child.text
get_tail(child)
get_tail(root)
import xml.etree.cElementTree as ET
data = ET.parse('test.xml')
for d in data.iter():
if d.tag in ["GOAL1", "GOAL2", "stepCC", "stepCC"]:
print d.text
elif d.tag in ["GOAL3", "GOAL4"]:
print d.attrib.values()[0]