Production Patterns
Production XML Cookbook
Field-tested architectural patterns for high-throughput streaming, memory bounding, and security shielding.
Gigabyte-Scale Streaming
When processing multi-gigabyte XML data sets (such as Wikipedia XML dumps, OpenStreetMap feeds, or financial transaction logs), building an in-memory DOM inevitably leads to out-of-memory crashes.
oxml provides Reader::next_borrowed(), which streams events while borrowing string slices directly from the internal input buffer with zero heap allocation overhead.
Pattern: Zero-Copy Entity Extraction
use ;
use File;
use BufReader;
Denial of Service Shielding
Legacy C parsers like libxml2 are famously susceptible to XML Entity Expansion attacks ("Billion Laughs" and quadratic blowout), where recursive DTD entity expansions multiply memory consumption exponentially until the host runs out of RAM.
Billion Laughs Mitigation
oxml defends against entity expansion denial-of-service by design:
- Entity Expansion Recursion Limits: Recursive general entity replacement is strictly bounded.
- Generational Depth Caps: Nesting levels are bounded to prevent stack overflow.
- Pure Memory Bounding: Because no raw pointer indirection exists, memory cannot be fragmented or held hostage by circular references.
use Reader;
// Attack payload: nested entity expansions
let attack_xml = r#"<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
]>
<root>&lol3;</root>"#;
let mut reader = from_str;
// Guaranteed: Safe termination with descriptive error, zero memory explosion.
Complex Namespace Traversal
In XML 1.0, elements without a prefix in a namespaced document belong to the default namespace (xmlns="http://example.com/ns"). According to the W3C XPath 1.0 specification, unprefixed names in an XPath expression only match elements in no namespace.
The Problem in Legacy Code
In lxml or standard tools, querying a document with a default namespace using //entry returns 0 results, confusing developers:
Hello
The oxml Solution
Bind an explicit prefix to the namespace URI. Both oxml-cli and the Rust API bind prefixes explicitly so that expressions remain stable regardless of document prefix aliases:
CLI Approach:
Rust API Approach:
use Document;
use Context;
let xml = r#"<feed xmlns="http://www.w3.org/2005/Atom"><title>Release</title></feed>"#;
let doc = parse?;
let mut ctx = new;
ctx.register_namespace;
let nodes = doc.select_with_context?;
assert_eq!;
W3C XML Schema (XSD) Validation Pipeline
Validate documents against complex enterprise XSD definitions in automated pipelines:
use Validator;
let schema_source = read_to_string?;
let validator = from_str?;
let xml_input = read_to_string?;
match validator.validate