XML and JSON in Python

NPFL092 Technology for Natural Language Processing

XML and JSON in Python

Zdenk Zabokrtsk?, Rudolf Rosa

December 1, 2020

Charles University Faculty of Mathematics and Physics Institute of Formal and Applied Linguistics

unless otherwise stated

XML in Python

? the two standard approaches for XML processing are supported in the standard library: ? xml.dom.* ? a standard DOM API (Document Object Model) ? xml.sax.* ? a standard SAX API (Simple Api for Xml)

? but there's xml.etree.ElementTree (ET for short) ? a lightweight Pythonic API ? supports both DOM-like (but ET faster than DOM) and SAX-like processing (i.e. event-based, streaming i.e. all-in-memory) ? fast C implementation used by default whenever possible in Python3 (no need for xml.etree.cElementTree as in Python 2)

Credit: The following slides are based on an ElementTree intro by Eli Bendersky.

1/ 20

An XML file as a tree

credit:

2/ 20

ET: loading an XML doc

loading from a file: import xml.etree.ElementTree as ET tree = ET.ElementTree(file='sample.xml') or from a string: root = ET.fromstring('')

3/ 20

ET: traversing the tree

root = tree.getroot() for child in root:

print(child.tag, child.attrib, child.text) for descendant in root.iter():

....

4/ 20

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download