2.12. Read XML

  • File paths works also with URLs

  • io.StringIO Converts str to File-like object

  • pd.read_xml()

2.12.1. SetUp

>>> import pandas as pd
>>> from io import StringIO
>>>
>>> pd.set_option('display.width', 250)
>>> pd.set_option('display.max_columns', 20)
>>> pd.set_option('display.max_rows', 30)

2.12.2. Read XML

>>> DATA = """<?xml version="1.0"?>
... <catalog>
...    <book id="bk101">
...       <author>Gambardella, Matthew</author>
...       <title>XML Developer's Guide</title>
...       <genre>Computer</genre>
...       <price>44.95</price>
...       <publish_date>2000-10-01</publish_date>
...       <description>An in-depth look at creating applications
...       with XML.</description>
...    </book>
...    <book id="bk102">
...       <author>Ralls, Kim</author>
...       <title>Midnight Rain</title>
...       <genre>Fantasy</genre>
...       <price>5.95</price>
...       <publish_date>2000-12-16</publish_date>
...       <description>A former architect battles corporate zombies,
...       an evil sorceress, and her own childhood to become queen
...       of the world.</description>
...    </book>
...    <book id="bk103">
...       <author>Corets, Eva</author>
...       <title>Maeve Ascendant</title>
...       <genre>Fantasy</genre>
...       <price>5.95</price>
...       <publish_date>2000-11-17</publish_date>
...       <description>After the collapse of a nanotechnology
...       society in England, the young survivors lay the
...       foundation for a new society.</description>
...    </book>
... </catalog>
... """
>>> data = StringIO(DATA)
>>>
>>> pd.read_xml(data)
      id                author                  title     genre  price publish_date                                        description
0  bk101  Gambardella, Matthew  XML Developer's Guide  Computer  44.95   2000-10-01  An in-depth look at creating applications\n   ...
1  bk102            Ralls, Kim          Midnight Rain   Fantasy   5.95   2000-12-16  A former architect battles corporate zombies,\...
2  bk103           Corets, Eva        Maeve Ascendant   Fantasy   5.95   2000-11-17  After the collapse of a nanotechnology\n      ...

2.12.3. XML and XSLT

>>> from lxml.etree import XML, XSLT, parse
>>> from io import StringIO
>>>
>>>
>>> TEMPLATE = """
...     <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
...         <table>
...             <thead>
...                 <tr>
...                     <th>Id</th>
...                     <th>Author</th>
...                     <th>Title</th>
...                     <th>Genre</th>
...                     <th>Price</th>
...                     <th>Publish Date</th>
...                     <th>Description</th>
...                 </tr>
...             </thead>
...             <tbody>
...
...                 <xsl:for-each select="catalog/book">
...                     <tr>
...                         <td><xsl:value-of select="@id"/></td>
...                         <td><xsl:value-of select="author"/></td>
...                         <td><xsl:value-of select="title"/></td>
...                         <td><xsl:value-of select="genre"/></td>
...                         <td><xsl:value-of select="price"/></td>
...                         <td><xsl:value-of select="publish_date"/></td>
...                         <td><xsl:value-of select="description"/></td>
...                     </tr>
...                 </xsl:for-each>
...
...             </tbody>
...         </table>
...     </html>
... """
>>>
>>> transform = XSLT(XML(TEMPLATE))
>>> data = parse(StringIO(DATA))
>>> html = StringIO(str(transform(data)))
>>> dfs = pd.read_html(html)
>>> result = dfs[0]
>>>
>>> result
      Id                Author                  Title     Genre  Price Publish Date                                        Description
0  bk101  Gambardella, Matthew  XML Developer's Guide  Computer  44.95   2000-10-01  An in-depth look at creating applications  wit...
1  bk102            Ralls, Kim          Midnight Rain   Fantasy   5.95   2000-12-16  A former architect battles corporate zombies, ...
2  bk103           Corets, Eva        Maeve Ascendant   Fantasy   5.95   2000-11-17  After the collapse of a nanotechnology  societ...
>>>
>>> type(result) is pd.DataFrame
True
>>>
>>> len(result) > 0
True
>>>
>>> result.columns
Index(['Id', 'Author', 'Title', 'Genre', 'Price', 'Publish Date', 'Description'], dtype='object')
>>>
>>> result['Title']
0    XML Developer's Guide
1            Midnight Rain
2          Maeve Ascendant
Name: Title, dtype: object

2.12.4. Assignments

# FIXME: Rozbić na trzy zadania, jedno z read_xml(file), read_xml(StringIO), oraz inne z mean

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Pandas Read XML
# - Difficulty: medium
# - Lines: 1
# - Minutes: 3

# %% English
# 1. Read data from `DATA` as `pd.DataFrame`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Wczytaj dane z `DATA` jako `pd.DataFrame`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `pip install --upgrade lxml`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> pd.set_option('display.width', 250)
>>> pd.set_option('display.max_columns', 20)
>>> pd.set_option('display.max_rows', 30)

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.DataFrame, \
'Variable `result` has invalid type, should be `pd.DataFrame`'

>>> result
           common               botanical  zone         light     price  availability
0       bloodroot  sanguinaria canadensis     4  mostly shady  2.40 USD         31599
1       columbine    aquilegia canadensis     3  mostly shady  9.40 USD         30699
2  marsh marigold        caltha palustris     4  mostly sunny  6.80 USD         51799
3         cowslip        caltha palustris     4  mostly shady  9.90 USD         30699
"""
from io import StringIO

import pandas as pd

DATA = """
    <catalog>
        <plant>
            <common>bloodroot</common>
            <botanical>sanguinaria canadensis</botanical>
            <zone>4</zone>
            <light>mostly shady</light>
            <price>2.40 USD</price>
            <availability>031599</availability>
        </plant>
        <plant>
            <common>columbine</common>
            <botanical>aquilegia canadensis</botanical>
            <zone>3</zone>
            <light>mostly shady</light>
            <price>9.40 USD</price>
            <availability>030699</availability>
        </plant>
        <plant>
            <common>marsh marigold</common>
            <botanical>caltha palustris</botanical>
            <zone>4</zone>
            <light>mostly sunny</light>
            <price>6.80 USD</price>
            <availability>051799</availability>
        </plant>
        <plant>
            <common>cowslip</common>
            <botanical>caltha palustris</botanical>
            <zone>4</zone>
            <light>mostly shady</light>
            <price>9.90 USD</price>
            <availability>030699</availability>
        </plant>
    </catalog>
"""


# read DATA from XML
# type: pd.DataFrame
result = ...