Parsing and querying XML
Chapter lab: XML
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch16-xml.zip && unzip -q ch16-xml.zip && rm ch16-xml.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch16-xml.zip -OutFile ch16-xml.zip; Expand-Archive ch16-xml.zip -DestinationPath .; Remove-Item ch16-xml.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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”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} PenDone 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 BackpackTask 2 — Run it green
Section titled “Task 2 — Run it green”Run:
uv run robot checksrobot checkspoetry run robot checksDone 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.
Which keyword parses an XML file?
The XML library's
Parse XML <path-or-string>returns the root element.Show the answer
Answer:
Parse XML— The XML library'sParse XML <path-or-string>returns the root element.Get Element ${root} product[1]/nameselects…product[1]picks the first product;Get Elementrequires exactly one match, so index when there are many.Show the answer
Answer: the first product's name —
product[1]picks the first product;Get Elementrequires 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.