Use this when parsing XML in PowerShell or Python, including namespaces such as in NETCONF replies.
Parsing · XML
Parse XML with a proper library — [xml] or Select-Xml in PowerShell, lxml or ElementTree in Python — taking care of namespaces, which NETCONF replies always use.
1. Use safe XML parsers (defusedxml, lxml with resolve_entities=False).
2. Disable DTD/external entities.
3. Handle encoding.
4. Stream large files (iterparse).
5. Validate after parsing.
XML → safe parser → tree
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 encoding/BOM issues, delimiter/quoting ambiguity, duplicate keys, namespace drift, scalar coercion, invalid schema assumptions and parser success without consumer-valid semantics, choosing the cases most relevant to decoding/lexing/structural parse, schema validation, error location and preventing parser success from being treated as business-semantic validity.
Step 2 Common mistakes Narrow the fault
- Parsing untrusted XML with unsafe parsers.
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 [xml], Select-Xml, lxml or ElementTree.
- Register namespaces before querying.
- Avoid regex for XML.
python3 -c 'import defusedxml.ElementTree as ET;ET.parse("f.xml")'— safe parsexmllint --noout --nonet file.xml— no network entity loading- XXE protections enabled — security
ReferenceWhy it matters1 section · depth on demand
In depth
Parsing XML reads the document into a tree that code can navigate and query. Choose the parser for the job: ElementTree (standard library) for most cases, lxml for XPath and speed, streaming parsers (iterparse) for very large files.
from defusedxml.ElementTree import fromstring # safe for untrusted input
root = fromstring(response_text)
[xml]$x = Get-Content export.xml
$x.SelectNodes('//vlan') | ForEach-Object { $_.id, $_.name }
Common problems: namespaces not passed to queries (finds nothing), encoding declarations that do not match the bytes, and XML External Entity (XXE) attacks when untrusted XML is parsed with entity resolution enabled.
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 XML > Parsing. 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 decoding/lexing/structural parse, schema validation, error location and preventing parser success from being treated as business-semantic validity, prefer typed fields and stable IDs over human presentation text.