Skip to main content

Light-weight, simple and fast XML parser with XPath support

Project description

Python bindings for pugixml

PyPI PyPI - Python Version PyPI - License pre-commit.ci status tests build

pugixml is a light-weight C++ XML processing library. It features:

  • DOM-like interface with rich traversal/modification capabilities
  • Extremely fast non-validating XML parser which constructs the DOM tree from an XML file/buffer
  • XPath 1.0 implementation for complex data-driven tree queries
  • Full Unicode support with Unicode interface variants and automatic encoding conversions

Documentation

Example

Loading XML document from file:

from pugixml import pugi
doc = pugi.XMLDocument()
result = doc.load_file('xgconsole.xml')
if not result:
    print('parse error: status=%r description=%r' % (result.status, result.description()))

Searching for nodes/attributes with predicates:

tools = doc.child('Profile').child('Tools')

# Find child via predicate (looks for direct children only)
node = tools.find_child(lambda x: x.attribute('AllowRemote').as_bool())
print(node.attribute('Filename').value())

# Find node via predicate (looks for all descendants in depth-first order)
node = doc.find_node(lambda x: x.attribute('AllowRemote').as_bool())
print(node.attribute('Filename').value())

# Find attribute via predicate
attr = tools.last_child().find_attribute(lambda x: x.name() == 'AllowRemote')
print(attr.value())

Selecting nodes via XPath expression:

tools = doc.select_nodes('/Profile/Tools/Tool[@AllowRemote="true" and @DeriveCaptionFrom="lastparam"]')
for tool in tools:
    print(tool.node().attribute('Filename').value())

Using query objects and variables:

varset = pugi.XPathVariableSet()
var = varset.add('remote', pugi.XPATH_TYPE_BOOLEAN)
query_remote_tools = pugi.XPathQuery('/Profile/Tools/Tool[@AllowRemote = string($remote)]', varset)

var.set(True)
tools_remote = query_remote_tools.evaluate_node_set(doc)
for tool in tools_remote:
    tool.node().print(pugi.PrintWriter())

var.set(False)
tools_local = query_remote_tools.evaluate_node_set(doc)
for tool in tools_local:
    tool.node().print(pugi.PrintWriter())

Installation

Installing a package from PyPI

pip install pugixml

Building a package from source

  • Requirements:

  • Installing a package from PyPI:

    pip install --no-binary=:all: pugixml
    
  • Installing the development version from the git repository:

    pip install git+https://github.com/miute/pugixml-python.git
    

License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pugixml-0.6.0.tar.gz (350.1 kB view hashes)

Uploaded Source

Built Distributions

pugixml-0.6.0-cp312-cp312-win_amd64.whl (263.0 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

pugixml-0.6.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (376.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pugixml-0.6.0-cp312-cp312-macosx_12_0_x86_64.whl (270.0 kB view hashes)

Uploaded CPython 3.12 macOS 12.0+ x86-64

pugixml-0.6.0-cp312-cp312-macosx_12_0_arm64.whl (242.3 kB view hashes)

Uploaded CPython 3.12 macOS 12.0+ ARM64

pugixml-0.6.0-cp311-cp311-win_amd64.whl (260.7 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

pugixml-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (376.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pugixml-0.6.0-cp311-cp311-macosx_12_0_x86_64.whl (262.3 kB view hashes)

Uploaded CPython 3.11 macOS 12.0+ x86-64

pugixml-0.6.0-cp311-cp311-macosx_12_0_arm64.whl (240.4 kB view hashes)

Uploaded CPython 3.11 macOS 12.0+ ARM64

pugixml-0.6.0-cp310-cp310-win_amd64.whl (260.2 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pugixml-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (374.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pugixml-0.6.0-cp310-cp310-macosx_12_0_x86_64.whl (260.9 kB view hashes)

Uploaded CPython 3.10 macOS 12.0+ x86-64

pugixml-0.6.0-cp310-cp310-macosx_12_0_arm64.whl (239.0 kB view hashes)

Uploaded CPython 3.10 macOS 12.0+ ARM64

pugixml-0.6.0-cp39-cp39-win_amd64.whl (242.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pugixml-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (375.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pugixml-0.6.0-cp39-cp39-macosx_12_0_x86_64.whl (261.0 kB view hashes)

Uploaded CPython 3.9 macOS 12.0+ x86-64

pugixml-0.6.0-cp39-cp39-macosx_12_0_arm64.whl (239.1 kB view hashes)

Uploaded CPython 3.9 macOS 12.0+ ARM64

pugixml-0.6.0-cp38-cp38-win_amd64.whl (259.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pugixml-0.6.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (374.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pugixml-0.6.0-cp38-cp38-macosx_12_0_x86_64.whl (260.5 kB view hashes)

Uploaded CPython 3.8 macOS 12.0+ x86-64

pugixml-0.6.0-cp38-cp38-macosx_12_0_arm64.whl (260.5 kB view hashes)

Uploaded CPython 3.8 macOS 12.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page