Concepts / Introduction to XML Structure

Introduction to XML Structure

Parsing is the first step and searching is the second. fromstring creates the structure; methods such as find work on that parsed structure.

  • Programming
Interactive lab

Try it: XML Element Trees

How ET.fromstring() turns XML text into a tree of Elements (tag, attributes, .text), and how find(), findall() and iter() walk that tree along a path such as users/user.

How it works

  1. The parser reads start tags, text and end tags left to right, keeping a stack of open elements; each start tag becomes an Element that is a child of the element on top of the stack.
  2. Text before an element's first child becomes its .text (whitespace included); an end tag must match the open element or parsing stops with ParseError.
  3. findall(path) starts at the element it is called on (the root is not part of the path) and follows each path segment down one level of children.
  4. find(path) returns the first match or None; iter(tag) visits every element in document order, including the root.
  5. Reading .text, .tag or .get('attr') on each result gives the extracted values; reading an attribute from None raises AttributeError.

Default run (29 steps): ET.fromstring() receives 183 characters. The parser reads tags left to right and keeps a stack of open elements. … findall() returned a list of 2 elements; reading e.get('x') from each gives ['2', '7'].

Simplified: A small, hand-written XML parser for a subset: elements, attributes, text, self-closing tags, comments, CDATA, processing instructions and the five predefined entities plus numeric character references. DOCTYPE/DTDs and namespaces are not supported. Error messages follow Python's expat parser for these cases (column numbers can differ for non-ASCII documents). Paths support tags, *, ., //, [@attr], [@attr='v'], [tag] and [n].

Educational simulation

Loading the simulation…

From Flat Text to a Tree

XML data often arrives from a web service, a file, or another system as a flat string of characters. Although that string contains tags, nesting, text, and attributes, Python cannot conveniently search it as a hierarchy until it has been parsed. ElementTree, Python's built-in XML library, provides fromstring for this transformation.

ET.fromstring reads the XML string, interprets its tags, nesting, and attributes, and builds an in-memory tree object. The outermost tag becomes the root element. Elements inside it become child elements, so the parsed result represents the XML document as a nested hierarchy that Python code can query.

person XML<person>...</person>nametext contentpersonroot elementemailhide attributefromstringcontainscontains
How does the nested XML text become a tree of parent and child elements that can be navigated?

Reading Parent and Child Relationships

After parsing, the XML is represented as a hierarchy. The root is the outermost element. An element may contain child elements, text content, and attributes. In a document describing a person, the person element can contain children such as name and email. Elements at the same level are siblings because they share the same parent.

personrootnametextemailhideparent ofparent of
What contains what in the XML structure, and how are elements connected to their parent and sibling elements?
Key point

Parsing is the first step and searching is the second. fromstring creates the structure; methods such as find work on that parsed structure.

Finding an Element by Tag

The find method searches the tree for the first element matching a supplied tag name. It returns an element object when a match is found. If no matching element exists, it returns None. This means find does not directly return the element's text; it returns the element so that your code can then inspect its text or attributes.

personrootnamefirst matching elementemaildifferent tagfind("name")sibling
How does find move through the XML hierarchy to locate a specific element?
python

In this example, fromstring parses the XML string into a tree represented by root. Calling root.find("name") searches that tree for the first name element. Calling root.find("email") searches for the first email element. The variables name_element and email_element refer to the located element objects, not yet to their text values.

Reading Text and Attributes

Worked example

Extracting Person Details

Parse a person XML string, locate the name and email elements, and extract the name text and the email hide attribute.

  1. 1
    Parse

    Pass the XML string to ET.fromstring so ElementTree builds a hierarchical, queryable representation.

  2. 2
    Locate

    Use find("name") and find("email") to locate the first elements with those tag names.

  3. 3
    Read text

    Use the name element's .text property to access the text inside the name tag.

  4. 4
    Read attribute

    Use the email element's .get("hide") method to retrieve the value of the hide attribute.

Result

The name text is Ada, and the email hide attribute is yes.

Code walkthrough
name_text = name_element.text
hide_value = email_element.get("hide")
print(name_text)
print(hide_value)
  1. 1
    name_element.text reads the text content of the located name element.
  2. 2
    email_element.get("hide") retrieves the value associated with the hide attribute.
  3. 3
    The print statements display the extracted values.
Output
Ada
yes
emailelement objectada@example.com.textyes.get("hide")textattribute
Where are an element's text value and attributes located in the parsed tree, and how are they accessed?

Checking Search Results

  • Common mistake

    Treating find as though it returns text directly.

    What it looks like
    name = root.find("name") and then using name as if it were the text value.
    Why it fails

    find returns an element object or None, not a string.

    Do this instead

    Use name.text after locating the element.

  • Common mistake

    Ignoring the possibility of None.

    What it looks like
    result = root.find("phone") followed immediately by result.text when no phone element is present.
    Why it fails

    find returns None when no element matches the requested tag.

    Do this instead

    Handle the no-match case before trying to read the result.

  • Common mistake

    Changing the spelling or capitalization of a tag or attribute.

    What it looks like

    Searching for "Name" when the XML tag is "name".

    Why it fails

    XML tag and attribute names are case-sensitive.

    Do this instead

    Match the exact spelling used in the XML.

Practice the Parse-and-Search Process

EASY

Given the XML string <book><title>XML Basics</title><format type="digital">guide</format></book>, describe the tree created by ET.fromstring. Then identify which expression would retrieve the title text and which expression would retrieve the format element's type attribute.

Hints
  • The outermost book tag is the root element.
  • Use find with the exact tag name to locate an element.
  • Use .text for text content and .get("type") for the type attribute.
  1. 1
    The XML string is the starting text form, not yet a convenient searchable hierarchy. ET.fromstring parses it into a tree with a root and nested child elements. find searches that tree for the first matching tag and returns an element object or None. After locating an element, .text reads its text content and .get("attribute_name") retrieves an attribute value.

Key Takeaways

  • ET.fromstring transforms an XML string into a hierarchical tree structure.
  • The root is the outermost element, and nested elements form parent-child relationships.
  • find searches for the first element matching an exact tag name and returns an element object or None.
  • Use .text for element text and .get("attribute_name") for an attribute value.
  • XML tag and attribute names are case-sensitive.

More concepts starting with I