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.
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
- 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.
- 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.
- 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.
- find(path) returns the first match or None; iter(tag) visits every element in document order, including the root.
- 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].
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.
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.
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.
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
Extracting Person Details
Parse a person XML string, locate the name and email elements, and extract the name text and the email hide attribute.
- 1Parse
Pass the XML string to ET.fromstring so ElementTree builds a hierarchical, queryable representation.
- 2Locate
Use
find("name")andfind("email")to locate the first elements with those tag names. - 3Read text
Use the name element's .text property to access the text inside the name tag.
- 4Read attribute
Use the email element's .get("hide") method to retrieve the value of the hide attribute.
The name text is Ada, and the email hide attribute is yes.
name_text = name_element.text
hide_value = email_element.get("hide")
print(name_text)
print(hide_value)- 1
name_element.text reads the text content of the located name element. - 2
email_element.get("hide")retrieves the value associated with the hide attribute. - 3The print statements display the extracted values.
Ada
yesChecking Search Results
Common mistake Treating find as though it returns text directly.
What it looks likename = root.find("name") and then using name as if it were the text value.Why it failsfind returns an element object or None, not a string.
Do this insteadUse name.text after locating the element.
Common mistake Ignoring the possibility of None.
What it looks likeresult = root.find("phone") followed immediately by result.text when no phone element is present.Why it failsfind returns None when no element matches the requested tag.
Do this insteadHandle 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 likeSearching for "Name" when the XML tag is "name".
Why it failsXML tag and attribute names are case-sensitive.
Do this insteadMatch the exact spelling used in the XML.
Practice the Parse-and-Search Process
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.
- 1The 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.