Use this when Python parses XML with ElementTree or lxml, including namespaces.
XML · Python Automation
Python parses XML with xml.etree.ElementTree from the standard library or lxml for XPath and namespaces, as needed for NETCONF replies and other XML APIs.
1. Parse XML with defusedxml (XXE protection) or lxml with safe settings.
2. Namespaces matter.
3. XPath for extraction.
4. Validate with XSD where available.
5. Used by NETCONF.
XML → safe parser → tree → XPath
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as environment drift, implicit coercion, broad exception handling, blocking/async confusion, shell quoting/subprocess risks, partial writes and non-deterministic external APIs, choosing the cases most relevant to XML syntax/namespace/encoding plus schema/consumer expectations; preserve qualified names and distinguish well-formedness from application validity.
Step 2 Common mistakes Narrow the fault
- Parsing untrusted XML with unsafe parsers (use defusedxml).
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Use ElementTree or lxml.
- Handle namespaces explicitly.
- lxml supports full XPath.
python3 -c 'import defusedxml.ElementTree as ET;ET.parse("f.xml")'— safe XML parsexmllint --noout f.xml— well-formed check- XPath queries tested — extraction
ReferenceWhy it matters1 section · depth on demand
In depth
Python reads and writes XML with xml.etree.ElementTree (standard library), lxml (faster, full XPath) and defusedxml (safe parsing of untrusted input). XML appears in NETCONF replies, vendor exports and some APIs.
from defusedxml.ElementTree import fromstring
ns = {"if": "urn:ietf:params:xml:ns:yang:ietf-interfaces"}
root = fromstring(reply_xml)
for intf in root.iterfind(".//if:interface", ns):
print(intf.findtext("if:name", namespaces=ns), intf.findtext("if:enabled", namespaces=ns))
For NETCONF work, ncclient returns replies that can be parsed this way, and xmltodict converts XML to dictionaries when you prefer working with dict-like data.
ReferenceImportant terms & boundaries1 section · depth on demand
Mechanics
The linked authority for bounded mechanics is W3C — Extensible Markup Language (XML) 1.0; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Python Automation > XML. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.w3.org/TR/xml/ · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For XML syntax/namespace/encoding plus schema/consumer expectations; preserve qualified names and distinguish well-formedness from application validity, prefer typed fields and stable IDs over human presentation text.