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:
events— theEventenum and itsBytes*payloads (BytesStart,BytesText, …), plus theattributesiterator.name—QName,LocalName,Prefix,Namespaceand theNamespaceResolver.escape—escape/unescapeand friends, including attribute-value normalization.encoding— theDecoder(UTF-8 always; non-UTF-8 behind theencodingfeature, backed byoxixml-encoding).errors—Error,Result,SyntaxErrorandIllFormedError.reader— the pull-basedReaderand itsConfig, plus the namespace-awareNsReaderthat resolves prefixes to namespaces.writer— the events-basedWriterand itsElementWriterbuilder.parser— the low-level incremental parsers that theReaderdrives (ElementParser,CommentParser,PiParser).
§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 < b & c");
assert_eq!(unescape("a < b & c").expect("valid"), "a < b & c");Re-exports§
pub use crate::encoding::Decoder;pub use crate::errors::serialize::DeError;serializepub use crate::errors::serialize::SeError;serializepub 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§
- de
serialize - Serde
Deserializermodule. - 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.
- se
serialize - Serde serializer that turns Rust values into XML.
- serde_
helpers serde-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_ enum serde-types - A helper to implement
Deserializefor internally tagged enums which does not useDeserializer::deserialize_anythat produces wrong results with XML because of serde#1183.
Enums§
- XmlVersion
- Version of the XML standard.