Working with XML Data
Module 2
Parsing and extracting data from XML documents using XPath and Python libraries for data analysis
© 2026 WayUp
XML = eXtensible Markup Language
Every XML document forms a tree hierarchy (like folders on your computer):
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with...</description>
<calories>650</calories>
</food>
</breakfast_menu>
<breakfast_menu>: the root element<food>: a child elementXPath is like a GPS for navigating XML documents:
/ = one level down | // = search anywhere | [1] = first item only
Python has built-in XML support:
import xml.etree.ElementTree as xmlReader
# read from xml
tree = xmlReader.parse('menu.xml')
root = tree.getroot()
# iterate through elements
for food in root.findall('food'):
name = food.find('name').text
print(name)
from lxml import etree
# read from xml
tree = etree.parse('menu.xml')
root = tree.getroot()
print(root)
elems = root.findall('./food')
data = [[elem.find("./name").text,
elem.find("./price").text
] for elem in elems]
print(data)
# [['Belgian Waffles', '$5.95'], ['French Toast', '$4.50']]
This creates a list of lists - perfect for DataFrame conversion!
/: looks from the root./: path is relative to current position./food[starts-with(./name/text(), 'Be')]
It is possible to bind "raw data" in Orange tables:
from Orange.data import *
data = [
['green', 4, 1.2, 'apple'],
['orange', 5, 1.1, 'orange'],
['yellow', 4, 1.0, 'peach']
]
color = DiscreteVariable('color', values=set([row[0] for row in data]))
calories = ContinuousVariable('calories')
fiber = ContinuousVariable('fiber')
fruit = DiscreteVariable('fruit', values=set([row[3] for row in data]))
domain = Domain([color, calories, fiber], class_vars=fruit)
table = Table.from_list(domain, data)
out_dataArchive: ml.zip - ML articles in XML
glob.glob("ml/*.xml") to list all XML files.
Working with XML Data
Next: REST APIs and Web Services
© 2026 WayUp - way-up.io