1 Answer
30
import xml.etree.ElementTree as ET
# Parse XML file
tree = ET.parse('data.xml')
root = tree.getroot()
# Parse XML string
root = ET.fromstring(xml_string)
# Find elements
for child in root:
print(child.tag, child.attrib)
# Find specific elements
for item in root.findall('.//item'):
name = item.find('name').text
print(name)
# Using XPath
elements = root.findall('.//item[@type="book"]')
F
Silver
•
169 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer