Skip to main content

pygixml: Python Giant XML

Python Versions PyPI version License: MIT Build Status Documentation Status GitHub Stars

pygixml (Python Giant XML) is a Cython framework built on two specialized C++ engines: pugixml for its in-memory DOM parser (XPath, objectify, dictify), and an inlined yxml push parser for true constant-memory streaming. Between the two, pygixml covers everything lxml and xmltodict do: dotted objectify navigation, XPath 1.0, and an xmltodict-compatible dictify, plus a streaming layer neither of them has, which is what makes pygixml the package of choice for big XML and big-data pipelines.

📚 View Full Documentation


Why pygixml?

Speed, memory, and size. pygixml brings pugixml's battle-tested C++ parser directly to Python.

Every parsing/conversion layer pygixml ships (raw DOM, dictify, objectify, jsonify) is measured against lxml, ElementTree, xmltodict, and xmljson in a full, reproducible benchmark suite (see benchmarks/), not summarized here as a handful of numbers that inevitably go stale the moment the code changes. The live, interactive report (refreshed on a schedule, not hand-edited) is on the benchmarks page.

Two things worth knowing going in, because they're structural (true regardless of which exact numbers the report shows this week), not a one-time measurement:

  • jsonify.stream_dump holds memory flat as input grows. Every DOM-based approach (pygixml's own DOM path included, lxml, xmltodict) allocates roughly in proportion to the input, so its memory use grows with file size. stream_dump never builds a tree at all, so it doesn't: this is a different complexity class (O(1) vs O(n)), not a fixed "X times less" you'd see at only one input size. It costs something in return: see the report's own memory panel for the honest trade-off, not just the part that flatters it.
  • Package size: pygixml installs in well under a megabyte, without vendoring a build of libxml2/libxslt the way lxml does.

Built for big XML

Those benchmark numbers are for documents that fit comfortably in memory. For the documents that don't (multi-gigabyte exports, logs, data dumps), pygixml's streaming layer is the part lxml and xmltodict simply don't have:

  • pygixml.iterfind / dictify.iterdict / jsonify.iterjsonl: yxml-based incremental parsing in constant memory: one element (or one dict, or one JSON line) in flight at a time, regardless of whether the source document is 10 KB or 10 GB.

  • jsonify.stream_dump(xml_path, json_path) (the headline feature) converts a giant XML file into a single, valid, giant JSON file, entirely in C++, in constant memory, with an xmltodict-compatible output shape (same @attr / #text / repeated-siblings-as-array conventions as dictify.parse). No DOM tree is ever built, no intermediate Python dict/list/str is ever allocated, and the file never has to fit in RAM: only an in-place seek-and-patch trick on the output file is used to close JSON arrays correctly as repeated siblings are discovered. As far as we know, this is the only Python package that can do this without buffering the document, the output, or both, and without crashing the process once the file gets genuinely large.

    The memory guarantee is unconditional (measured: ~17MB peak, completely flat from 5K to 320K records, see the memory panel of the benchmarks page). The time is linear in the input size for every shape, including two or more different tags interleaving at the same level (<a>1</a><b>1</b><a>2</a><b>2</b>...): such siblings are moved into place once, in one pass, when their parent closes.

  • jsonify.stream_jsonl(xml_path, jsonl_path, tag): the file-to-file counterpart of iterjsonl (filters by tag, unlike stream_dump which always converts the whole document): streams straight to a .jsonl file, one matched element per line, same constant-memory, all-C++ guarantee.

from pygixml import jsonify

# A multi-GB XML file in, a multi-GB JSON file out: peak memory stays flat.
jsonify.stream_dump("huge_export.xml", "huge_export.json")

# Or, one record per line:
jsonify.stream_jsonl("huge_export.xml", "huge_export.jsonl", "record")

What is where

Capability API Section
In-memory DOM, XPath 1.0 parse_string, parse_file, XMLDocument, XMLNode, XPathQuery Quick Start, XPath Support
Dotted navigation objectify objectify
XML to dict (xmltodict-compatible) dictify dictify
XML to JSON, JSON Lines, constant memory jsonify (dumps*, stream_dump, stream_jsonl) Built for big XML
Streaming parse iterparse, iterfind, dictify.iterdict, jsonify.iterjsonl, PullParser Built for big XML
HTML to XML and back parse_html, html_to_xml, xml_to_html, encode_name, decode_name HTML to XML and back
Tree diff, patch, equality diff, patch, is_equal Diff, patch and structural similarity
Structural embeddings signature, similarity Diff, patch and structural similarity
Command line pygixml cat, query, jsonify, stream, convert, diff, similarity Command Line Tools

Features

  • Fast parsing: pugixml's C++ DOM parser, consistently at or near the front across the benchmark suite (see the benchmarks page)
  • Constant-memory streaming: jsonify.stream_dump holds flat regardless of input size; nothing else compared here does
  • Small footprint: well under a megabyte installed, no bundled libxml2/libxslt
  • Full XPath 1.0: complete query engine with all standard functions
  • Pythonic API: intuitive properties and methods, not a direct C++ mirror
  • objectify: lxml.objectify-style dotted navigation
  • dictify: xmltodict-compatible XML → dict conversion
  • jsonify: direct XML → JSON, in memory or streamed straight to disk in constant memory (stream_dump, stream_jsonl)
  • HTML ↔ XML: parse_html / html_to_xml turn real-world HTML into well-formed XML you can query with XPath, xml_to_html goes back; names XML can't hold are mapped reversibly (encode_name / decode_name)
  • Tree diff / patch: an edit script between two XML or HTML documents (insert, delete, move, update text with an optional word-level diff, set/delete attribute), patch to apply it in place, is_equal for a one-pass "did anything change?": see Diff, patch and structural similarity
  • Structural embeddings: signature (a pq-gram or path bag as a sparse {hash: count} vector) and similarity (Dice, 0..1) for near-duplicate and template detection, or as a cheap pre-filter before diff
  • Streaming (iterfind, iterdict, iterjsonl): constant-memory, yxml-based incremental parsing for documents too big to load whole
  • CLI tools: pygixml cat (pretty-print/colorize), pygixml query (XPath/dotted query), pygixml jsonify (convert to JSON), pygixml convert (XML/JSON/YAML/TOON, any direction), pygixml stream (filter giant files in bounded memory), pygixml diff and pygixml similarity (compare two XML/HTML files): see Command Line Tools
  • Cross-platform: Windows, Linux, macOS
  • Text extraction: recursive text gathering with configurable joins
  • XML serialization: output with custom indentation
  • Node iteration: depth-first traversal of the entire document

Installation

# From PyPI
pip install pygixml

# Or from GitHub
pip install git+https://github.com/MohammadRaziei/pygixml.git

Quick Start

import pygixml

# Parse XML from string
xml = """
<library>
    <book id="1" category="fiction">
        <title>The Great Gatsby</title>
        <author>F. Scott Fitzgerald</author>
        <year>1925</year>
    </book>
    <book id="2" category="fiction">
        <title>1984</title>
        <author>George Orwell</author>
        <year>1949</year>
    </book>
</library>
"""

doc = pygixml.parse_string(xml)
root = doc.root                           # <library>

# Access children and attributes
book = root.child("book")
print(book.name)                          # book
print(book.attribute("id").value)         # 1
print(book.child("title").text())         # The Great Gatsby

# XPath queries
fiction = root.select_nodes("book[@category='fiction']")
print(f"Found {len(fiction)} fiction books")

# Create & save
doc = pygixml.XMLDocument()
root = doc.append_child("catalog")
root.append_child("item").set_value("Hello")
doc.save_file("output.xml")

Properties vs Methods

A quick reference so you don't get tripped up:

Properties (no ()) Methods (need ())
node.name, node.value, node.type node.child(name)
node.parent, node.next_sibling node.first_child()
node.xml, node.xpath node.append_child(name)
attr.name, attr.value node.set_value(v)
doc.root node.select_nodes(query)
node.first_attribute()
node.text()

objectify: dotted navigation

pygixml.objectify provides an lxml.objectify-inspired interface for navigating XML with plain Python attribute access.

from pygixml import objectify

xml = """
<database name="users_db" version="1.2">
    <user-profile id="101" verified="true">
        <first_name>Mohammad</first_name>
        <balance>450.75</balance>
    </user-profile>
    <entry>Value A</entry>
    <entry>Value B</entry>
</database>
"""

root = objectify.from_string(xml)

# Dotted navigation: underscores map to hyphens automatically
print(root.user_profile.first_name)        # ObjectifiedElement(<first_name>)
print(str(root.user_profile.first_name))   # 'Mohammad'

# Automatic type inference for attributes
print(root.version)                        # 1.2   (float)
print(root.user_profile.id)               # 101   (int)
print(root.user_profile.verified)         # True  (bool)

# Text content
print(str(root.user_profile.first_name))  # 'Mohammad'   always str
print(root.user_profile.balance())        # 450.75        type-inferred

# Repeated siblings: indexing and iteration
print(root.entry[0])                      # ObjectifiedElement
print([str(e) for e in root.entry])       # ['Value A', 'Value B']

# Safe attribute access: never raises
print(root.get('version'))                # 1.2
print(root.get('missing', 'default'))     # 'default'

# Search descendants
print(root.find('balance'))               # ObjectifiedElement(<balance>)
print(root.find('balance', recursive=False))  # None  (not a direct child)
print(root.findall('entry'))              # [ObjectifiedElement, ...]

# Write support: modify in place
root.user_profile.first_name = "Ali"      # update child element text
root.version = 2.0                        # update attribute
root.timeout = 30                         # create new child element

# Delete
del root.timeout                          # remove child element
del root.version                          # remove attribute

objectify API

Feature Behaviour
root.child_tag First <child_tag> element; falls back to <child-tag>
root.attr_name Attribute value (type-inferred) when no child matches
root.tag[n] Index into repeated siblings
for e in root.tag Iterate repeated siblings
str(elem) Raw text content, always str
elem() Type-inferred text content
elem.get(name, default) Safe attribute read, never raises
elem.find(tag) First matching descendant, or None
elem.findall(tag) All matching descendants
elem.name = value Update child text or attribute; create child if absent
del elem.name Remove child element or attribute
elem.tag XML tag name string
elem.attrib {name: typed_value} dict of all attributes
elem.xml Serialised XML of the subtree
Child beats attribute When both share a name, child wins (read and write)

dictify: XML to dict

pygixml.dictify converts XML to a nested dict, compatible with the xmltodict library.

from pygixml import dictify

xml = """
<database name="users_db" version="1.2">
    <user-profile id="101" verified="true">
        <first_name>Mohammad</first_name>
        <balance>450.75</balance>
    </user-profile>
    <entry>Value A</entry>
    <entry>Value B</entry>
</database>
"""

# Parse XML → dict
d = dictify.parse(xml)
# {
#   'database': {
#     '@name': 'users_db',
#     '@version': '1.2',
#     'user-profile': {
#       '@id': '101', '@verified': 'true',
#       'first_name': 'Mohammad', 'balance': '450.75'
#     },
#     'entry': ['Value A', 'Value B']
#   }
# }

# Repeated siblings → list automatically
print(d['database']['entry'])             # ['Value A', 'Value B']

# Attributes prefixed with '@'
print(d['database']['@name'])             # 'users_db'

# Custom options
d = dictify.parse(xml,
    attr_prefix='',       # no prefix: attrs and children in same namespace
    cdata_key='text',     # key for text content (default '#text')
    force_list={'entry'}, # always a list, even with one element
    cdata_separator='',   # join text split by child elements (default: first run)
)

# Parse from file
d = dictify.parse_file('data.xml')

# Convert back to XML
xml_out = dictify.unparse(d, pretty=True, indent='\t')
print(xml_out)

dictify API

Parameter Default Description
attr_prefix "@" Prefix added to attribute keys
cdata_key "#text" Key for text content in mixed nodes
force_list None Tag names always wrapped in a list; pass True for all
cdata_separator None Text split by child elements (Hi<br/>x): None keeps the first non-blank run, a string joins all runs ("" gives "Hix")
strip_whitespace False Strip text at both ends (parse / parse_file only)
Function Description
dictify.parse(xml, **opts) Parse XML string → dict
dictify.parse_file(path, **opts) Parse XML file → dict
dictify.unparse(d, pretty, indent, ...) dict → XML string


HTML to XML and back

import pygixml

# HTML -> well-formed XML, and back
pygixml.html_to_xml("<ul><li>a<li>b<br></ul>")
# '<ul><li>a</li><li>b<br/></li></ul>'
doc = pygixml.parse_html("<ul><li>a<li>b<br></ul>")      # a normal XMLDocument
pygixml.xml_to_html(doc, unwrap="root")
# '<ul><li>a</li><li>b<br></li></ul>'

# Names XML can't hold (o:p, @click, [prop] ...) are mapped reversibly with the
# SQL/XML rules; `case` picks lower-case (default), "keep" or "escape"
pygixml.html_to_xml("<o:p @Click=1>x</o:p>", case="keep")
# '<o_x003A_p _x0040_Click="1">x</o_x003A_p>'
pygixml.decode_name("_x0040_Click")                       # '@Click'

A document from parse_html remembers it came from HTML, so to_html() restores the original names; a document from parse_string keeps its names as they are. Because it is an ordinary XMLDocument, XPath, dictify, jsonify, objectify and diff work on it too.

See the HTML page of the documentation for the options.


Diff, patch and structural similarity

Compare two ordered trees (XML, or HTML parsed with parse_html), apply the difference, or measure how alike their structure is. Everything runs in C++ inside the extension, one call per function, with no extra dependency.

import pygixml

a = pygixml.parse_html("<ul class='m'><li>Home<li>About us<li>Blog</ul>")
b = pygixml.parse_html("<ul class='m'><li>Home<li>About<li>Blog<li>Contact</ul>")

for action in pygixml.diff(a, b, text_diff=True):
    print(action)
# Insert(parent='/ul[1]', position=3, xml='<li>Contact</li>')
# UpdateText(path='/ul[1]/li[2]/text()[1]', text='About', old='About us',
#            diff=[(0, 'About'), (-1, ' us')])

pygixml.is_equal(a, b)           # False
pygixml.patch(a, pygixml.diff(a, b))     # applies the script to `a`, in place
pygixml.is_equal(a, b)           # True

diff: an edit script

diff(a, b) returns a list, empty when the trees are equivalent, of these actions, in the order they must be applied:

Action Meaning
Insert(parent, position, xml) add a node (a wholly new subtree is one action)
InsertText(parent, position, text) add a text node
Delete(path) remove a node with everything under it
Move(path, parent, position) move a subtree to another place
UpdateText(path, text, old, diff) change a text or comment node; diff is a word-level diff when text_diff=True
SetAttrib(path, name, value, old) set an attribute (old is None when it is new)
DeleteAttrib(path, name, old) remove an attribute

Paths look like /ul[1]/li[2]/text()[1] and refer to the tree as it is when that action runs. The script is verified inside the call: applying it to a must give b, otherwise PygiXMLDiffError is raised.

What counts as "the same" is a normalisation step, not a different algorithm:

Option Default Effect
ignore_whitespace True drop blank text, trim and collapse runs of whitespace
ignore_comments True comments are not compared
ignore_attrs () attribute names to ignore; "data-*" is a prefix
class_as_set True class="b a a" equals class="a b"
keys ("id",) attributes that identify an element wherever it sits
moves True pair unmatched subtrees across parents (Move)
text_diff False attach a word-level diff (also CJK and Persian) to UpdateText

Attribute order never matters, and elements are never renamed (a different tag is a delete plus an insert).

x = pygixml.parse_string("<r><i id='1'>a</i><i id='2'>b</i><i id='3'>c</i></r>")
y = pygixml.parse_string("<r><i id='3'>c</i><i id='1'>a2</i><i id='2'>b</i></r>")
pygixml.diff(x, y)
# [Move(path='/r[1]/i[3]', parent='/r[1]', position=0),
#  UpdateText(path='/r[1]/i[2]/text()[1]', text='a2', old='a', diff=None)]

patch and is_equal

  • patch(doc, actions) applies a script to a document in place. Every action checks what it expects (the path exists, the old text, the old attribute value), so a script is refused with PygiXMLDiffError when the document is not the one it was made for. Pass the same normalisation options you gave diff. It may leave the document partly patched: patch a copy if you need all-or-nothing.
  • is_equal(a, b, **options) answers "did anything change?" in one pass by comparing subtree hashes, so it is the cheap check in front of a diff.

Structural embeddings: signature and similarity

signature(node) turns the structure of a tree into a sparse vector, {hash: count}, in time linear in the size of the tree. similarity(a, b) is the Dice coefficient of two such vectors, from 0 (nothing shared) to 1 (same structure), computed in one C++ call without building the dicts.

pygixml.similarity(a, b)                      # 0.889: structure only
pygixml.similarity(a, b, content=True)        # 0.711: text counts too
pygixml.similarity(a, b, method="paths")      # 0.889: cruder and cheaper

sig = pygixml.signature(a)                    # {330417701356879974: 1, ...}
  • method="pqgram" (default): pq-grams, windows of p ancestors and q consecutive children around every node (Augsten, Bohlen, Gamper, 2010). Their distance approximates the tree edit distance.
  • method="paths": the bag of root-to-node tag paths. Cruder, good for telling page templates apart.
  • Text is one constant label unless content=True, so by default two pages built from the same template are as similar as can be, whatever they say.

Typical uses: near-duplicate and template detection across many pages, and a cheap pre-filter before an expensive diff.

The matching algorithm (Chawathe et al., 1996), the limits and measured speeds are in the diff documentation.


Command Line Tools

pygixml installs one CLI entry point, pygixml, with seven subcommands: no Python code required for one-off queries, conversions, comparing two files, or filtering giant files from a shell pipeline. Every subcommand also works as python -m pygixml <subcommand>.

Subcommand Loads Use it for
pygixml cat full DOM pretty-print (and colorize) an XML file for the terminal: the XML analog of bat/jq -C
pygixml query full DOM ad-hoc XPath / dotted queries: a jq/xq-style tool for XML you can fit in memory
pygixml jsonify (json alias) DOM or streamed converting a whole file to JSON, .json in / out
pygixml stream one record at a time filtering matches out of a file too big to load, bounded memory
pygixml convert full DOM converting between XML, JSON, YAML, TOON and HTML, any direction (--case for HTML names)
pygixml diff two full DOMs the edit script between two XML or HTML files, like diff for trees
pygixml similarity two full DOMs one number, 0..1, for how alike two files are structurally

pygixml cat: pretty-print and colorize

pygixml cat data.xml                    # pretty + colorized (auto: only
                                         # on a real terminal, and only
                                         # if colorama is installed)
cat data.xml | pygixml cat -            # from stdin
pygixml cat data.xml --color always     # force color even when piped
pygixml cat data.xml --color never      # force plain, no color
pygixml cat data.xml --indent 4         # 4-space indent instead of 2
pygixml cat data.xml -o pretty.xml      # write to a file (never colorized
                                         # by 'auto': a file isn't a tty)

python -m pygixml cat data.xml

Colorizing is fully optional: install it with pip install pygixml[all] (pulls in colorama, along with YAML/TOON support for pygixml convert). Without it, cat still works exactly the same, just without color: plain, pretty-printed XML, same as --color never.

pygixml query

# XPath (starts with / or //)
pygixml query data.xml "//user-profile[@id='101']/first_name"

# dotted, objectify-style (starts with .): first segment names the root
pygixml query data.xml ".database.user_profile.first_name"
pygixml query data.xml ".database.user_profile.@id"      # attribute
pygixml query data.xml ".database.entry[1]"              # index
pygixml query data.xml ".database.entry[*]"              # all siblings
pygixml query data.xml ".database.user_profile.text()"   # text content

# output formats
pygixml query data.xml ".database" --format xml
pygixml query data.xml ".database" --format json --pretty

# just the count, exit code like grep (0 = match, 1 = no match)
pygixml query data.xml ".database.entry[*]" --count

# multiple files, stdin, NUL-separated for xargs -0
pygixml query *.xml ".config.host"
cat data.xml | pygixml query - ".config.host"
pygixml query data.xml ".database.entry[*]" --null | xargs -0 -n1 echo

# python -m form works identically
python -m pygixml query data.xml ".database.user_profile.first_name"

pygixml jsonify: convert a whole file

pygixml jsonify data.xml                        # compact JSON to stdout
pygixml jsonify data.xml -p                     # pretty (2-space indent)
pygixml jsonify data.xml -o data.json           # write to a file
pygixml jsonify data.xml --force-list item      # always make <item> a list
cat data.xml | pygixml jsonify -                # read from stdin

python -m pygixml jsonify data.xml -o data.json
pygixml json data.xml                           # `json` is a short alias

Automatically switches to jsonify.stream_dump (constant memory) for files over 64MB; --stream / --no-stream force one mode or the other regardless of size.

pygixml stream: filter a giant file

Reads the file once via iterparse, tag by tag, holding at most one matched element's subtree in memory: the right tool once a file is too large for pygixml query/pygixml jsonify's DOM mode.

# every <order>, one JSON object per line (JSONL)
pygixml stream orders.xml --tag order

# simple filters: numeric or string, on a child path or an @attribute
pygixml stream orders.xml --tag order --where "total>100"
pygixml stream orders.xml --tag order --where "@status=shipped"

# multiple --where are AND'ed together
pygixml stream orders.xml --tag order \
    --where "@status=shipped" --where "customer=acme"

# a real JSON array instead of JSONL: still one pass, still bounded memory
pygixml stream orders.xml --tag order --where "total>100" --format array -p

# just count, or stop after N
pygixml stream orders.xml --tag order --where "@status=shipped" --count
pygixml stream orders.xml --tag order --limit 10

cat orders.xml | pygixml stream - --tag order
python -m pygixml stream orders.xml --tag order

--where supports =, !=, >, <, >=, <=; the left-hand side is either @attr or a child path like customer / items/item/sku (same syntax as StreamElement.findtext).



pygixml convert: XML, JSON, YAML, TOON, any direction

pygixml convert data.xml -o data.json         # format guessed from extensions
pygixml convert data.xml -o data.yaml
pygixml convert data.json -o data.xml -p      # pretty XML
pygixml convert data.yaml --to toon           # stdout needs --to explicitly
cat data.xml | pygixml convert - --from xml --to json   # stdin needs --from too

python -m pygixml convert data.xml -o data.yaml

Built on :class:pygixml.formats.FormatDocument, a small class wrapping a plain dict (the same @attr/#text/list shape dictify.parse produces), with to_json/to_xml/to_yaml/to_toon and matching from_* classmethods. JSON and XML always work (stdlib json + this package's own dictify); YAML needs PyYAML and TOON needs ctoon (also written by the author of pygixml). Both are optional, and asking for one you don't have gives a clear pip install ... error instead of crashing:

pip install pygixml[all]   # colorama (for `cat`) + PyYAML + ctoon

Like cat/query, this loads the whole document: for XML too big to fit in memory, convert with pygixml jsonify --stream instead.

pygixml diff and pygixml similarity: compare two files

pygixml diff old.html new.html
# insert   /ul[1] @3  <li>Contact</li>
# update   /ul[1]/li[2]/text()[1]  "About us" -> "About"

pygixml diff old.html new.html --text-diff     # About[- us-]: a word-level diff
pygixml diff a.xml b.xml --ignore-attrs 'data-*' --key id
pygixml diff a.xml b.xml --json | jq '.[] | select(.action == "Delete")'
pygixml diff a.html b.html -q || echo "changed"   # no output, exit 1 when they differ

pygixml similarity page1.html page2.html          # 0.889: structure only
pygixml similarity a.html b.html --content --method paths

A and B are XML or HTML files (the format comes from the extension, or --from xml|html; - reads stdin for one of them). diff prints one line per action (insert, delete, move, update, set, unset), or the same actions as a JSON list with --json. The normalisation options of the Python diff are flags: --ignore-attrs, --keep-whitespace, --keep-comments, --no-class-set, --key, --no-moves.

Exit codes of diff: 0 when done, 1 for "they differ" only with --exit-code or -q, 2 for trouble (unreadable file, input that does not parse, bad option). similarity prints the score with three decimals. Both load the two documents in memory.

Advanced Features

Text Content Extraction

import pygixml

xml = """
<root>
    <simple>Hello World</simple>
    <nested>
        <child>Child Text</child>
        More text
    </nested>
    <mixed>Text <b>with</b> mixed <i>content</i></mixed>
</root>
"""

doc = pygixml.parse_string(xml)
root = doc.root

print(root.child("simple").text())                # Hello World
print(root.child("nested").text(join=" | "))      # Child Text | More text
print(root.child("mixed").text(recursive=False))  # Text

XML Serialization

import pygixml

doc = pygixml.XMLDocument()
root = doc.append_child("root")
root.append_child("item").set_value("content")

print(root.xml)
# <root>
#   <item>content</item>
# </root>

print(root.to_string("    "))  # 4-space indent

Document Iteration

import pygixml

doc = pygixml.parse_string("<root><a/><b/></root>")

for node in doc:
    print(f"{node.type:12s} {node.name}")
# document
# element       root
# element       a
# element       b

Modifying XML

import pygixml

doc = pygixml.parse_string("<person><name>John</name></person>")
root = doc.root

root.child("name").set_value("Jane")
root.child("name").name = "full_name"
root.append_child("age").set_value("30")

print(root.xml)
# <person>
#   <full_name>Jane</full_name>
#   <age>30</age>
# </person>

XPath Support

Full XPath 1.0 via pugixml's engine:

import pygixml

xml = """
<library>
    <book id="1" category="fiction">
        <title>The Great Gatsby</title>
        <author>F. Scott Fitzgerald</author>
        <year>1925</year>
        <price>12.99</price>
    </book>
    <book id="2" category="fiction">
        <title>1984</title>
        <author>George Orwell</author>
        <year>1949</year>
        <price>10.99</price>
    </book>
</library>
"""

doc = pygixml.parse_string(xml)
root = doc.root

# Select nodes
books = root.select_nodes("book")
print(f"Found {len(books)} books")

# Predicates
fiction = root.select_nodes("book[@category='fiction']")
print(f"Found {len(fiction)} fiction books")

# Single node
book = root.select_node("book[@id='2']")
if book:
    print(book.node.child("title").text())    # 1984

# Pre-compiled query for repeated use
query = pygixml.XPathQuery("book[year > 1930]")
recent = query.evaluate_node_set(root)
print(f"Found {len(recent)} books published after 1930")

# Scalar evaluations
avg = pygixml.XPathQuery("sum(book/price) div count(book)").evaluate_number(root)
print(f"Average price: ${avg:.2f}")           # Average price: $11.99

has_orwell = pygixml.XPathQuery("book[author='George Orwell']").evaluate_boolean(root)
print(f"Has Orwell books: {has_orwell}")       # Has Orwell books: True

Supported XPath

Category Examples
Node selection //book, /library/book, book[1]
Attributes book[@id], book[@category='fiction']
Boolean ops and, or, not()
Comparisons =, !=, <, >, <=, >=
Math +, -, *, div, mod
Functions position(), last(), count(), sum(), string(), number()
Axes child::, attribute::, descendant::, ancestor::
Wildcards *, @*, node()

Core API

Class / Module Purpose
XMLDocument Document-level operations: load, save, append-child
XMLNode Navigate, read, and modify individual nodes
XMLAttribute Attribute name and value access
XPathQuery Pre-compiled XPath queries for repeated evaluation
XPathNode Single XPath result (wraps a node or attribute)
XPathNodeSet Collection of XPath results
objectify lxml.objectify-style dotted navigation
dictify xmltodict-compatible XML → dict conversion
jsonify Direct XML → JSON: in-memory dumps*, or constant-memory stream_dump/stream_jsonl
iterfind / iterparse yxml-based constant-memory streaming parser, ElementTree-style
pygixml cat, query, jsonify, convert, stream, diff, similarity CLI tools: see Command Line Tools

Module-level functions: parse_string(xml), parse_file(path), parse_html(html), html_to_xml(html), xml_to_html(doc), and diff / patch / is_equal / similarity / signature (see HTML to XML and back and Diff, patch and structural similarity).


Benchmarks

cmake -S benchmarks -B benchmarks/build
cmake --build benchmarks/build --target pygixml_bench_report

Compares pygixml's parse/dictify/objectify/jsonify against lxml, ElementTree, xmltodict, and xmljson: speed, memory at scale, and install size; parse_html against lxml.html, BeautifulSoup, html5lib and selectolax (speed and peak memory); and the command-line tools against xq. Writes the raw numbers to benchmarks/results/*.json; the docs build (sphinx_docs) renders the interactive page from it. See benchmarks/README.md for the full methodology, or the live copy on the benchmarks page.


Documentation

📖 Full docs: https://mohammadraziei.github.io/pygixml/


Security

pygixml is not affected by the common XML attacks (XXE, Billion Laughs, external DTD retrieval) that lxml/defusedxml guard against, because its two embedded parsers, pugixml (DOM API) and yxml (streaming API), never implement custom entity resolution or external DTD fetching in the first place, so there's no "safe mode" to configure. See #8 for the full technical breakdown.


License

MIT License: see LICENSE.

Enjoy pygixml? Star the repository ⭐ 👉 Star pygixml on GitHub


Acknowledgments

  • pugixml: Fast and lightweight C++ XML library
  • yxml: Tiny, dependency-free streaming XML parser, powering pygixml's constant-memory streaming layer
  • Cython: C extensions for Python
  • scikit-build: Modern Python build system

Metadata

Release files for pygixml 0.15.3

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

Source distribution (sdist)

Source distribution for pygixml 0.15.3
File Size Uploaded Provenance Actions
pygixml-0.15.3.tar.gz 964.6 kB Without provenance Details for pygixml-0.15.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for pygixml 0.15.3
File Actions
pygixml-0.15.3-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for pygixml-0.15.3-cp310-abi3-win_amd64.whl
pygixml-0.15.3-cp310-abi3-win32.whl CPython 3.10 abi3 Windows x86-32 Details for pygixml-0.15.3-cp310-abi3-win32.whl
pygixml-0.15.3-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for pygixml-0.15.3-cp310-abi3-musllinux_1_2_x86_64.whl
pygixml-0.15.3-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pygixml-0.15.3-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pygixml-0.15.3-cp310-abi3-macosx_10_9_universal2.whl CPython 3.10 abi3 macOS 10.9+ universal2 (ARM64, x86-64) Details for pygixml-0.15.3-cp310-abi3-macosx_10_9_universal2.whl
pygixml-0.15.3-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details for pygixml-0.15.3-cp39-cp39-win_amd64.whl
pygixml-0.15.3-cp39-cp39-win32.whl CPython 3.9 CPython 3.9 Windows x86-32 Details for pygixml-0.15.3-cp39-cp39-win32.whl
pygixml-0.15.3-cp39-cp39-musllinux_1_2_x86_64.whl CPython 3.9 CPython 3.9 Linux musl 1.2+ x86-64 Details for pygixml-0.15.3-cp39-cp39-musllinux_1_2_x86_64.whl
pygixml-0.15.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pygixml-0.15.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pygixml-0.15.3-cp39-cp39-macosx_10_9_universal2.whl CPython 3.9 CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) Details for pygixml-0.15.3-cp39-cp39-macosx_10_9_universal2.whl
pygixml-0.15.3-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details for pygixml-0.15.3-cp38-cp38-win_amd64.whl
pygixml-0.15.3-cp38-cp38-win32.whl CPython 3.8 CPython 3.8 Windows x86-32 Details for pygixml-0.15.3-cp38-cp38-win32.whl
pygixml-0.15.3-cp38-cp38-musllinux_1_2_x86_64.whl CPython 3.8 CPython 3.8 Linux musl 1.2+ x86-64 Details for pygixml-0.15.3-cp38-cp38-musllinux_1_2_x86_64.whl
pygixml-0.15.3-cp38-cp38-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for pygixml-0.15.3-cp38-cp38-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
pygixml-0.15.3-cp38-cp38-macosx_10_9_universal2.whl CPython 3.8 CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) Details for pygixml-0.15.3-cp38-cp38-macosx_10_9_universal2.whl

Total release size: 14.0 MB

Release files / pygixml-0.15.3.tar.gz

Download URL pygixml-0.15.3.tar.gz
Size 964.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c9c6238ae37cc5839783e37908fb211fc16b26468d579f11113fdc71bce28f17
BLAKE2b-256 checksum
How to use checksums
3c15eba2acff3d7c13024e1e0caeb2133a5ec27d20e599493708e53b890416bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp310-abi3-win_amd64.whl

Download URL pygixml-0.15.3-cp310-abi3-win_amd64.whl
Size 533.9 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
1425ef8ad1466426ed13431d6b20858eef85c4424776576549b930d23f7e94ea
BLAKE2b-256 checksum
How to use checksums
b1496d86812e1b2955eaa53c5e86c0c19ef7c7a1a15379481c03dd81c7123579
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp310-abi3-win32.whl

Download URL pygixml-0.15.3-cp310-abi3-win32.whl
Size 449.2 kB
Tags CPython 3.10 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
a5b18d0b54bd6ec5e82317b30ae4b43a22a6f98acef3fe5313a8988621fb0ddd
BLAKE2b-256 checksum
How to use checksums
46793cbd84d63aa6c4c96dffb61ba0b8dddf1e457c7e9675f82d592e7f1f8014
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL pygixml-0.15.3-cp310-abi3-musllinux_1_2_x86_64.whl
Size 1.7 MB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
5941dc5526b885bbe806a620d14a6c6f1d05466622cdaf93d4ccc124d3cfa2a6
BLAKE2b-256 checksum
How to use checksums
fff662b54b7361010fb8aeeb1556d0927ea2fcd4a74cd1a188c46a5906bae46f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pygixml-0.15.3-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 645.1 kB
Tags CPython 3.10 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
ca25881519744cf15d9de1893e59aab458822729110b316a0760138d2b0a77ec
BLAKE2b-256 checksum
How to use checksums
cb6de2013d1f1da25616216874314201af18fe82950d7df1d197e61e55bb4069
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp310-abi3-macosx_10_9_universal2.whl

Download URL pygixml-0.15.3-cp310-abi3-macosx_10_9_universal2.whl
Size 978.6 kB
Tags CPython 3.10 abi3 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
7581a0badb534577e81b47475468af98498583eec72662c9c2d349ed3b4f45cc
BLAKE2b-256 checksum
How to use checksums
548f45ae8bb5321d216c60e61c4d2508fd3369d89ebfd0d71a0d2b17c5e0bb73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp39-cp39-win_amd64.whl

Download URL pygixml-0.15.3-cp39-cp39-win_amd64.whl
Size 543.5 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
9b994eba85ec1d97bdbe38c2f53c4fb587147e3b98df977909530967e2a8063f
BLAKE2b-256 checksum
How to use checksums
655c5ff79c6e1f4ad6765bff7709b851af6716f0be435748e51d42559afdd878
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp39-cp39-win32.whl

Download URL pygixml-0.15.3-cp39-cp39-win32.whl
Size 464.2 kB
Tags CPython 3.9 Windows x86-32
SHA-256 checksum
How to use checksums
095b4c444a8bd524837c93036396c35235bb6c0f9e621df8a47bb5f110934862
BLAKE2b-256 checksum
How to use checksums
0a645b696bb385e3d6fea1497b53ef9b5d30caed62511dbd179acad58819669b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp39-cp39-musllinux_1_2_x86_64.whl

Download URL pygixml-0.15.3-cp39-cp39-musllinux_1_2_x86_64.whl
Size 1.7 MB
Tags CPython 3.9 Linux musl 1.2+ x86-64
SHA-256 checksum
How to use checksums
f6dc60f0e896278507c8e47f16cc49b5baed843296dd9fdeab75cbe3fa7263fc
BLAKE2b-256 checksum
How to use checksums
6471f26a1f453eb09d4574988df3f1532a4b325ddfd9d9d026c76c220edd3a28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pygixml-0.15.3-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 697.8 kB
Tags CPython 3.9 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
a4e971197b47bedb201d259c6c874e2ae3c5e2d534abed555712da864101ffe4
BLAKE2b-256 checksum
How to use checksums
c772c6c509217c897894deb095ea0572911a8070063898247db902a9b6152273
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp39-cp39-macosx_10_9_universal2.whl

Download URL pygixml-0.15.3-cp39-cp39-macosx_10_9_universal2.whl
Size 1.0 MB
Tags CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
2bb8b656fd3837e4f423d304a03541174e01109617d8fae9866c4e454c0028f3
BLAKE2b-256 checksum
How to use checksums
33afcce900f6ab4b25fbb64694c99a9e69b526a047e639fd50ed0c7c4ecb15c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp38-cp38-win_amd64.whl

Download URL pygixml-0.15.3-cp38-cp38-win_amd64.whl
Size 512.7 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
669fd8210cb41a705ac12be2b024bc4f8e8dcc7f3fd01ab80c96dbee54f45746
BLAKE2b-256 checksum
How to use checksums
1b81561d55f50c31503e4d4c80de776e537a77002e483d89d92ca34ae8809358
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp38-cp38-win32.whl

Download URL pygixml-0.15.3-cp38-cp38-win32.whl
Size 436.1 kB
Tags CPython 3.8 Windows x86-32
SHA-256 checksum
How to use checksums
30812be20af6c15722c06936716166d54e58786b4cea96dc2133af35c7117bd5
BLAKE2b-256 checksum
How to use checksums
7597540f888721a7c3f49f37e5ff3bca5e229c82bc0ade4a5c128eec57cc3dc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp38-cp38-musllinux_1_2_x86_64.whl

Download URL pygixml-0.15.3-cp38-cp38-musllinux_1_2_x86_64.whl
Size 1.7 MB
Tags CPython 3.8 Linux musl 1.2+ x86-64
SHA-256 checksum
How to use checksums
9f8ef3bcbc8d7bb0eb1c500973748e1a6d413b6b61c943e13134f8debc845014
BLAKE2b-256 checksum
How to use checksums
7596a0599237a11d2ae5457e415f8ae57fc89187195fe8ad0bd13a8eba94162a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp38-cp38-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL pygixml-0.15.3-cp38-cp38-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 663.6 kB
Tags CPython 3.8 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
c6da1ca958d32653e9c7f4943d63af4055825fa8303568109fe98f8af0fea892
BLAKE2b-256 checksum
How to use checksums
110fab767e272199ea8dec3822f112e1413e77ee385778d870065f5380602389
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pygixml-0.15.3-cp38-cp38-macosx_10_9_universal2.whl

Download URL pygixml-0.15.3-cp38-cp38-macosx_10_9_universal2.whl
Size 1.0 MB
Tags CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
1092f47074884aaf20713b0158d03ac0e4b13aaa98537b333a68adc1848cb7f5
BLAKE2b-256 checksum
How to use checksums
447d5b4fe9580b6cf68c376509b36a782d74e9eb071028c304ae8962ee8fb2b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for pygixml 0.15.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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