Expand description
§Restricted XML parsing and encoding
This crate provides “restricted” parsing and encoding of XML 1.0 documents with namespacing.
§Features (some call them restrictions)
- No external resources
- No custom entities
- No DTD whatsoever
- No processing instructions
- No comments
- UTF-8 only
- Namespacing-well-formedness enforced
- XML 1.0 only
- Streamed parsing (parser emits a subset of SAX events)
- Streamed encoding
- Parser can be driven push- and pull-based
- Tokio-based asynchronicity supported via the
async
feature andAsyncReader
.
§Examples
§Parse data from byte slices
To parse a XML document from a byte slice (or a series of byte slices), you
can use the Parser
with the Parse
trait directly:
use rxml::{Parser, Parse, Error, ResolvedEvent, XmlVersion};
use std::io;
let mut doc = &b"<?xml version='1.0'?><hello>World!</hello>"[..];
let mut fp = Parser::new();
while doc.len() > 0 {
let ev = fp.parse(&mut doc, true); // true = doc contains the entire document
println!("got event: {:?}", ev);
}
§Parse data from a standard library reader
To parse a XML document from a std::io::BufRead
struct, you can use the
Reader
.
// let file = std::fs::File::open(..).unwrap();
let reader = BufReader::new(file);
let mut reader = rxml::Reader::<_>::new(reader);
let result = rxml::as_eof_flag(reader.read_all(|ev| {
println!("got event: {:?}", ev);
}));
assert_eq!(result.unwrap(), true); // true indicates eof
§Parse data using tokio
To parse a XML document from a tokio::io::AsyncBufRead
struct, you can use
the AsyncReader
.
This requires the async
feature.
use rxml::{AsyncReader, Error, ResolvedEvent, XmlVersion};
// let sock = ..;
let reader = tokio::io::BufReader::new(sock);
// this converts the doc into an tokio::io::AsyncRead
let mut reader = AsyncReader::<_>::new(reader);
// we expect the first event to be the XML declaration
let ev = reader.read().await;
assert!(matches!(ev.unwrap().unwrap(), ResolvedEvent::XmlDeclaration(_, XmlVersion::V1_0)));
§Feature flags
macros
: Enable macros to convert&str
to&NameStr
,&NcNameStr
and&CDataStr
respectively.smartstring
(default): Enable the use ofsmartstring
for some string types to avoid allocations and conserve heap memory.tokio
(default): EnableAsyncReader
and related types. Impliessync
.sync
(default): Use Arc instead of Rc for deduplicated namespace URIs.stream
: Add afutures::Stream
implementation toAsyncReader
. Impliestokio
.shared_ns
: Allow deduplication of namespace URIs within and across parsers.
Re-exports§
pub use strings::CData;
pub use strings::CDataStr;
pub use strings::Name;
pub use strings::NameStr;
pub use strings::NcName;
pub use strings::NcNameStr;
pub use bytes;
Modules§
- Error types
- Restricted XML 1.0 parsing facilities
- Strongly-typed strings for use with XML 1.0 documents
- Writer for restricted XML 1.0
Macros§
- xml_cdata
macros
Compile-time conversion of a string literal toCDataStr
- xml_name
macros
Compile-time conversion of a string literal toNameStr
- xml_ncname
macros
Compile-time conversion of a string literal toNcNameStr
Structs§
- AsyncReader
tokio
Tokio-compatible asynchronous restricted XML 1.0 parser - Shared context for multiple parsers
- Encodes XML into buffers.
- Parser configuration
- Non-blocking restricted XML 1.0 parser
- Low-level restricted XML 1.0 parser
- Restricted XML 1.0 parser
Enums§
- Error types which may be returned from the parser or lexer.
- An encodable item.
- Logical XML document parts
- High-level, logical XML document parts
- XML version number
Constants§
- The static prefix used for XML built-in attributes (
xml
). - The static prefix used for XML namespace declarations (
xmlns
). - Package version
- XML core namespace URI (for the
xml:
prefix) - XML namespace URI (for the
xmlns:
prefix)
Traits§
- XML parser trait
- WithContextDeprecatedCompatibility trait, use
WithOptions
instead. - Trait for things which can be constructed with
Options
.
Functions§
- Convert end-of-file-ness of a result to a boolean flag.
Type Aliases§
- Compatibility alias, use
Reader
instead. - FeedParserDeprecatedCompatibility alias, use
Parser
instead. - PullDriverDeprecated
- PullParserDeprecatedCompatibility alias, use
Reader
instead. - PushDriverDeprecated
- Pair of an optional namespace prefix and a localpart, commonly used in element and attribute names.
- Pair of an optional namespace name (URI) and a localpart, commonly used in element and attribute names.
- The default result type for this crate.