PyXB-X

1.2.6.3 · maintenance · verified Fri Apr 17

PyXB-X ("pixbix") is a pure Python package that generates Python source code for classes that correspond to data structures defined by XMLSchema. It is a re-release of PyXB 1.2.6 under a distinct namespace (`pyxb_x`) to avoid dependency conflicts. The project appears to be in maintenance mode, with its last PyPI release in March 2020 and its upstream PyXB project showing limited recent activity.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple XML Schema (XSD), use the `pyxbgen` command-line tool to generate Python binding classes, and then use those classes to create Python objects, serialize them to XML, and parse XML back into objects.

import os
import sys
import subprocess
import tempfile
import shutil

# 1. Define a simple XML Schema (XSD)
xsd_content = """<?xml version=\"1.0\"?>
<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">
  <xs:element name=\"person\">
    <xs:complexType>
      <xs:sequence>
        <xs:element name=\"name\" type=\"xs:string\"/>
        <xs:element name=\"age\" type=\"xs:int\"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
"""

# 2. Set up temporary directory for XSD and generated bindings
temp_dir = tempfile.mkdtemp()
xsd_path = os.path.join(temp_dir, 'example.xsd')
with open(xsd_path, 'w') as f:
    f.write(xsd_content)

binding_module_name = 'my_example_bindings'
output_dir = temp_dir

# 3. Use pyxbgen to generate Python bindings
#    The `pyxbgen` command-line tool is installed with `pyxb-x`.
#    If `pyxbgen` is not found in PATH, use `python -m pyxb_x.gen.pyxbgen`.

# Determine the correct command for pyxbgen
try:
    subprocess.check_call(['pyxbgen', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    pyxbgen_cmd = 'pyxbgen'
except (subprocess.CalledProcessError, FileNotFoundError):
    pyxbgen_cmd = [sys.executable, '-m', 'pyxb_x.gen.pyxbgen']

generate_command_args = [
    f'--schema-location={xsd_path}',
    '--binding-language=python',
    f'--module={binding_module_name}',
    f'--output-directory={output_dir}'
]

full_command = [pyxbgen_cmd] + generate_command_args if isinstance(pyxbgen_cmd, list) else [pyxbgen_cmd] + generate_command_args

print(f"Running generation command: {' '.join(full_command)}")
subprocess.check_call(full_command)
print(f"Bindings generated to: {output_dir}/{binding_module_name}.py")

# 4. Add the output directory to sys.path to import the generated module
sys.path.insert(0, output_dir)

# 5. Import and use the generated bindings
import my_example_bindings

# Create an instance of the 'person' element
p = my_example_bindings.person(name="Alice", age=30)

# Serialize to XML
xml_doc = p.toxml('utf-8', element_name='person')
print("\nGenerated XML:")
print(xml_doc.decode('utf-8'))

# Parse XML back into a Python object
parsed_p = my_example_bindings.CreateFromDocument(xml_doc)
print(f"\nParsed back: Name={parsed_p.name}, Age={parsed_p.age}")

# Clean up temporary files
shutil.rmtree(temp_dir)
print("\nCleaned up temporary files.")

view raw JSON →