Python Glossary Русский

all Python 3xml.etree.ElementTreeterm

xml

module

A package for parsing and producing XML: the ElementTree tree, DOM and streaming SAX.

Syntax

import xml.etree.ElementTree as ET

The package offers three approaches: xml.etree.ElementTree, a simple element tree, is the usual choice; xml.dom.minidom is the W3C DOM and more verbose; xml.sax parses huge files as a stream without loading them into memory. The package is not protected against maliciously crafted XML, so untrusted data is parsed with the third-party defusedxml. For configuration and data exchange between your own programs json is usually simpler.

Examples

import xml.etree.ElementTree as ET
root = ET.fromstring("<cart><item price='3'>tea</item><item price='5'>jam</item></cart>")
for item in root.iter("item"):
    print(item.text, item.get("price"))  # → tea 3 / jam 5

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy