Skip to content

Parsing and querying XML

Chapter lab: XMLch16-xml.zip · 5 KB

This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.

Download ch16-xml.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch16-xml.zip && unzip -q ch16-xml.zip && rm ch16-xml.zip

Open the folder in VS Code with code rfe-ch16-xml (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch16-xml/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • smoke.robot
    • Directorysample_data/
      • app.log
      • catalog.xml
      • orders.csv
      • results.xml
      • settings.cfg
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

The XML standard library parses a document and queries it with element paths. Parse XML returns the root; Get Elements selects many by path; Get Element selects exactly one — so when several match, index it, e.g. product[1]. Work against the bundled sample_data/catalog.xml.

Task 1 — Parse the catalog and query its elements

Section titled “Task 1 — Parse the catalog and query its elements”
Edit

Goal: create checks/xml.robot that parses the catalog, counts its product elements, and reads the name of the first one.

Here is the same parse-count-read flow over a small inline document. Adapt it to the catalog file.

Worked example — adapt it, don’t copy it

*** Settings ***
Library XML
*** Test Cases ***
Parse A Small Document
${root}= Parse XML <shop><item>Pen</item><item>Cup</item></shop>
${items}= Get Elements ${root} item
Length Should Be ${items} 2
${first}= Get Element ${root} item[1]
Should Be Equal ${first.text} Pen

Done when

the suite imports XML and defines Catalog Has Four Products.

Hint 1

Parse XML also takes a path: ${root}= Parse XML sample_data/catalog.xml. Then Get Elements ${root} product and Length Should Be ${products} 4.

Hint 2

Read the first product’s name with a nested path: Get Element ${root} product[1]/name, then assert Should Be Equal ${first.text} Trail Backpack.

Solution
*** Settings ***
Library XML
*** Test Cases ***
Catalog Has Four Products
${root}= Parse XML sample_data/catalog.xml
${products}= Get Elements ${root} product
Length Should Be ${products} 4
${first}= Get Element ${root} product[1]/name
Should Be Equal ${first.text} Trail Backpack
Run

Run:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

four products are found and the first is the Trail Backpack.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. Which keyword parses an XML file?

    Show the answer

    Answer: Parse XML — The XML library's Parse XML <path-or-string> returns the root element.

  2. Get Element ${root} product[1]/name selects…

    Show the answer

    Answer: the first product's name — product[1] picks the first product; Get Element requires exactly one match, so index when there are many.

Key terms

XMLconcept
Standard library to parse and query XML with element paths.
Parse XML / Get Element(s)keyword
Parse a document, then select one element or many by path.

All terms in the glossary →