Skip to main content

XMLGenerator

Rust Python XML GitHub uv Pytest GitHub Actions Test Rust Test Python Coverage Upload Read the Docs Documentation Status CodeCov Coverage Coffee License: MIT No AI

This project provides a Python package pyxmlgenerator, which generates XML instances matching the provided XML Schema (XSD) input. The package is written in Rust with PyO3 Python bindings allowing direct use from Python. The pyxmlgenerator package is a wrapper for the xmlgenerator Rust crate, which generates the XML from a given schema.

The project includes several additional Cargo crates to achieve this goal:

Additionally, the project also includes a Python wrapper for the xsdtestdata crate - pyxsdtestdata. This allows the test data to be accessed directly from Python.

Dependencies

System requirements:

  • Cargo - Installed via Rustup
  • libxml2-rs - Rust bindings for the libxml2 C library
  • Python - Required for Python bindings pyxmlgenerator
  • uv - Recommended for installation of Python packages (Optional)

Build

To build the Rust xmlgenerator library, simply run:

cargo build

To run the Rust test suite, use:

cargo test

To build the Python wrapper pyxmlgenerator, run:

uv sync --package pyxmlgenerator

To run the Python test suite, use:

uv run pytest

Usage

The library can be used either from Python or Rust. Here are examples of each:

Python

The following example illustrates a similar workflow in Python, using the built-in argparse and pathlib libraries to read and validate command-line input.

import pathlib
import argparse
import pyxmlgenerator

from xmlschema importXMLSchema

parser = argparse.ArgumentParser()
parser.add_argument("filepaths", type=pathlib.Path, nargs='+')

args = parser.parse_args()

paths = args.filepaths

xml_generator = pyxmlgeneratorXMLGenerator()

for path in paths:
    path_str = str(path)

    # Validate file
    try:
        xml_generator.validate(path_str)
    except pyxmlgenerator.XSDValidatorError:
        print(f"Invalid file: {path}")
        exit(0)

    print(f"Valid file: {path}")
    try:
        output = xml_generator.generate()
    except:
        print("Error running generator")
        exit(0)

    print("Output generated!")

    # Generate sc
    schema =XMLSchema(path_str)

    if schema.is_valid(output):
        print("Output is valid")
    else:
        print("Output does not match input schema")

This example uses Python's xmlschema library to validate output XML strings.

Rust

The following is an example of a simplified program which reads a list of filepaths from command-line arguments and generates example XML output strings for each valid XSD.

use std::env;
use std::path::PathBuf;
use std::str::FromStr;
use xmlgenerator:XMLGenerator;

fn main() {
    let generator =XMLGenerator::new();
    let args: Vec<String> = env::args().collect();
    if args.is_empty() {
        println!("No files provided!");
        println!("Usage: ./example [files] ...");
        return;
    }

    println!("Reading {} input files...", args.len());
    for arg in args {
        let path = PathBuf::from_str(arg.as_str()).unwrap();

        // Validate file
        if let Err(error) = generator.validate(&path) {
            println!("Invalid file: {}", path);
            return;
        }

        println!("Valid input file: {}", path);
        let output = match generator.generate(&path, None) {
            Ok(out) => out,
            Err(e) => {
                eprintln!("Error running generator: {}", path);
                eprintln!("Error: {}", e);
                return;
            }
        };

        println!("Output generated!");
        println!("Output:");
        println!(output);
    }
}

NOTE: Only a single XMLGenerator` should be created. Multiple instances of this class cause undefined behaviour.

Limitations

Not all features of the XSD specification have been implemented, if these features are encountered, an unimplemented error is thrown.

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyxmlgenerator-0.2.2-cp310-abi3-win_arm64.whl (2.2 MB view details)

Uploaded CPython 3.10+Windows ARM64

File details

Details for the file pyxmlgenerator-0.2.2-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: pyxmlgenerator-0.2.2-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.10+, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyxmlgenerator-0.2.2-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 93b59bc1a8acb06befe8c9cd0dbe62546eb7ef32bf6911e4d6738d2bac791e12
MD5 2a1e6e651a830698d781e90c679769ed
BLAKE2b-256 32e92bd4d18a2f4715c766a892de5142879682ffb336264b59e8b48ae5ac0874

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.4

1 file

0.2.3

1 file

This release

0.2.2 This release

1 file

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