Usage¶
doc = expatjson.parse_file("data.xml")
doc = expatjson.parse_gzip(payload)
doc = expatjson.parse_gzip_base64(text)
# raise the limits only for input you trust
doc = expatjson.parse(xml, max_size=50 * 1024 * 1024, max_depth=500)
# one element or many: the same code
for tag in expatjson.as_list(expatjson.get_path(doc, "book", "tag")):
print(tag)
print(expatjson.to_json(xml, indent=2))
expatjson data.xml --indent 2 -o data.json
cat data.xml | python -m expatjson - --lang en
Choosing the parser¶
The backend argument selects the parser. It also works with to_json, parse_file and parse_gzip.
| Value | Behaviour |
|---|---|
"auto" (default) |
use the C++ extension if present, otherwise Python |
"native" |
require the C++ extension; raise BackendUnavailableError if it cannot be loaded |
"python" |
always use the Python implementation |
Use "native" when a silent fallback is not acceptable, and "python" to compare behaviour or to narrow down a
problem.
doc = expatjson.parse("<a>1</a>", backend="python")
try:
doc = expatjson.parse("<a>1</a>", backend="native")
except expatjson.BackendUnavailableError as error:
print(error, error.hint())
expatjson data.xml --backend native # require the C++ extension
expatjson data.xml --backend python # force pure Python
EXPATJSON_BACKEND=python python your_app.py # never load the C++ extension