Crates.io | rxml |
lib.rs | rxml |
version | 0.12.0 |
source | src |
created_at | 2021-05-14 15:15:37.445297 |
updated_at | 2024-08-12 14:12:10.549199 |
description | Minimalistic, restricted XML 1.0 parser which does not include dangerous XML features. |
homepage | |
repository | https://codeberg.org/jssfr/rxml |
max_upload_size | |
id | 397420 |
size | 655,800 |
rxml
— Restricted, minimalistic XML 1.0 parserThis crate provides "restricted" parsing of XML 1.0 documents with namespacing.
async
feature and [AsyncReader
].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, Event, 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);
}
To parse a XML document from a [std::io::BufRead
] struct, you can use the
[Reader
].
# use std::io::BufReader;
# let file = &mut &b"<?xml version='1.0'?><hello>World!</hello>"[..];
// 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
tokio
To parse a XML document from a [tokio::io::AsyncBufRead
] struct, you can use
the [AsyncReader
].
This requires the tokio
feature.
# use tokio::io::AsyncRead;
use rxml::{AsyncReader, Error, Event, XmlVersion};
# tokio_test::block_on(async {
# let sock = &mut &b"<?xml version='1.0'?><hello>World!</hello>"[..];
// 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(), Event::XmlDeclaration(_, XmlVersion::V1_0)));
# })
macros
: Enable macros to convert &str
to &NameStr
, &NcNameStr
and
&CDataStr
respectively.compact_str
(default): Enable the use of
compact_str
for some string types
to avoid allocations and conserve heap memory.tokio
(default): Enable AsyncReader
and related types.stream
: Add a futures::Stream
implementation to AsyncReader
. Implies
tokio
.shared_ns
: Allow deduplication of namespace URIs within and across
parsers.