Skip to main content

Crate oxixml_xml

Crate oxixml_xml 

Source
Expand description

oxixml-xml — XML 1.0/1.1 pull parser and writer, quick-xml 0.41-compatible.

Part of the OxiXML workspace — the COOLJAPAN Pure-Rust foundation for all XML and RDF processing. This crate is a drop-in replacement for quick-xml: it mirrors the 0.41 public API (module paths, type names and method signatures) so that downstream projects can switch over with a one-line dependency change.

It provides the zero-copy event model, its supporting vocabulary, and the pull-based Reader:

§Conformance

Parsing is quick-xml-compatible — that is, deliberately permissive — by default. Setting Config::check_well_formedness (or Config::enable_all_checks) turns the reader into a conforming non-validating XML processor: the Char and Name productions, the document structure, the grammar of the XML declaration and of the internal DTD subset, and the entity-related well-formedness constraints are then all enforced, and violations surface as Error::NotWellFormed. Driven through an NsReader, the extra QName and prefix-binding constraints of Namespaces in XML are enforced as well.

use oxixml_xml::reader::Reader;

let mut reader = Reader::from_str("<a/><b/>");
reader.config_mut().check_well_formedness = true;
let _ = reader.read_event().expect("first root element");
assert!(reader.read_event().is_err(), "a document has a single root element");

With the serialize feature enabled, the crate additionally provides the serde deserializer in de (de::from_str, de::from_reader and de::Deserializer) and the matching serializer in se (se::to_string, se::to_string_with_root, se::to_writer and se::Serializer), which mirror the quick-xml 0.41 mapping rules (@field for attributes, $text and $value special names, sequences of consecutive elements, and so on).

§Example

use oxixml_xml::escape::{escape, unescape};
use oxixml_xml::events::{BytesStart, Event};
use oxixml_xml::name::QName;

// Build a start tag and inspect its name and attributes.
let start = BytesStart::new("greeting").with_attributes([("lang", "en")]);
assert_eq!(start.name(), QName(b"greeting"));
let (key, value) = {
    let attr = start.attributes().next().expect("one attribute").expect("valid");
    (attr.key.as_ref().to_vec(), attr.value.to_vec())
};
assert_eq!((key.as_slice(), value.as_slice()), (&b"lang"[..], &b"en"[..]));

// Wrap it in an event; events borrow their payload bytes.
let event = Event::Start(start);
assert_eq!(&*event, b"greeting lang=\"en\"".as_ref());

// Escape and unescape round-trip.
assert_eq!(escape("a < b & c"), "a &lt; b &amp; c");
assert_eq!(unescape("a &lt; b &amp; c").expect("valid"), "a < b & c");

Re-exports§

pub use crate::encoding::Decoder;
pub use crate::errors::serialize::DeError;serialize
pub use crate::errors::serialize::SeError;serialize
pub use crate::errors::Error;
pub use crate::errors::Result;
pub use crate::reader::NsReader;
pub use crate::reader::Reader;
pub use crate::writer::ElementWriter;
pub use crate::writer::Writer;

Modules§

deserialize
Serde Deserializer module.
encoding
Wrappers that decode byte data into strings.
errors
Error management for the crate.
escape
Manage XML character escapes.
events
Zero-copy XML events used throughout this library.
name
Handling of names according to the W3C Namespaces in XML 1.1 (Second Edition) specification.
parser
Contains low-level parsers of different XML pieces.
reader
High-level interface for the pull-based XML parser.
seserialize
Serde serializer that turns Rust values into XML.
serde_helpersserde-types
Helper functions and macros that glue XML to a serde content model.
writer
High-level interface for an events-based XML emitter.

Macros§

impl_deserialize_for_internally_tagged_enumserde-types
A helper to implement Deserialize for internally tagged enums which does not use Deserializer::deserialize_any that produces wrong results with XML because of serde#1183.

Enums§

XmlVersion
Version of the XML standard.