Skip to main content

Amara 3 XML

Python 3 tools for processing MicroXML, a simplification of XML. Amara 3 XML implements the MicroXML data model, and allows you to parse into this from tradiional XML and MicroXML.

The microx command line tool is especially useful for quick query and processing of XML.

Install

Requires Python 3.4+. Just run:

pip install amara3.xml

Use

Though Amara 3 is focused on MicroXML rather than full XML, the reality is that most of the XML-like data you’ll be dealing with is full XML 1.0. his package provides capabilities to parse legacy XML and reduce it to MicroXML. In many cases the biggest implication of this is that namespace information is stripped. As long as you know what you’re doing you can get pretty far by ignoring this, but make sure you know what you’re doing.

from amara3.uxml import xml

MONTY_XML = """<monty xmlns="urn:spam:ignored">
  <python spam="eggs">What do you mean "bleh"</python>
  <python ministry="abuse">But I was looking for argument</python>
</monty>"""

builder = xml.treebuilder()
root = builder.parse(MONTY_XML)
print(root.xml_name) #"monty"
child = next(root.xml_children)
print(child) #First text node: "

" child = next(root.xml_children) print(child.xml_value) #"What do you mean "bleh"" print(child.xml_attributes["spam"]) #"eggs"

There are some utilities to make this a bit easier as well.

from amara3.uxml import xml
from amara3.uxml.treeutil import *

MONTY_XML = """<monty xmlns="urn:spam:ignored">
  <python spam="eggs">What do you mean "bleh"</python>
  <python ministry="abuse">But I was looking for argument</python>
</monty>"""

builder = xml.treebuilder()
root = builder.parse(MONTY_XML)
py1 = next(select_name(root, "python"))
print(py1.xml_value) #"What do you mean "bleh""
py2 = next(select_attribute(root, "ministry", "abuse"))
print(py2.xml_value) #"But I was looking for argument"

Experimental MicroXML parser

For this parser the input truly must be MicroXML. Basics:

>>> from amara3.uxml.parser import parse
>>> events = parse('<hello><bold>world</bold></hello>')
>>> for ev in events: print(ev)
...
(<event.start_element: 1>, 'hello', {}, [])
(<event.start_element: 1>, 'bold', {}, ['hello'])
(<event.characters: 3>, 'world')
(<event.end_element: 2>, 'bold', ['hello'])
(<event.end_element: 2>, 'hello', [])
>>>

Or…And now for something completely different!…Incremental parsing.

>>> from amara3.uxml.parser import parsefrags
>>> events = parsefrags(['<hello', '><bold>world</bold></hello>'])
>>> for ev in events: print(ev)
...
(<event.start_element: 1>, 'hello', {}, [])
(<event.start_element: 1>, 'bold', {}, ['hello'])
(<event.characters: 3>, 'world')
(<event.end_element: 2>, 'bold

Implementation notes

Switched to a hand-crafted parser because:

  1. Worried about memory consumption of the needed PLY lexer
  2. Lack of incremental feed parse for PLY
  3. Inspiration from James Clark's JS parser https://github.com/jclark/microxml-js/blob/master/microxml.js

Author: Uche Ogbuji uche@ogbuji.net

Metadata

Release files for amara3.xml 3.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for amara3.xml 3.4.1
File Size Uploaded
amara3_xml-3.4.1.tar.gz 51.4 kB Details

Release files / amara3_xml-3.4.1.tar.gz

Download URL amara3_xml-3.4.1.tar.gz
Size 51.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6601bb0a48144133fccfa1f35f105f53a0240b1fffe0794f98ea8e27250402ab
BLAKE2b-256 checksum
How to use checksums
04310a52b8ec7fafea90f82b483e39b55b1a9e57cf6a0ea6db22d939e63d4912
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page