xml-rs, an XML library for Rust
xml-rs is an XML library for the Rust programming language. It supports reading and writing of XML documents in a streaming fashion (without DOM).
Features
-
XML spec conformance better than other pure-Rust libraries.
-
Easy to use API based on
Iterator
s and regularString
s without tricky lifetimes. -
Support for UTF-16, UTF-8, ISO-8859-1, and ASCII encodings.
-
Written entirely in the safe Rust subset. Designed to safely handle untrusted input.
The API is heavily inspired by Java Streaming API for XML (StAX). It contains a pull parser much like StAX event reader. It provides an iterator API, so you can leverage Rust's existing iterators library features.
It also provides a streaming document writer much like StAX event writer. This writer consumes its own set of events, but reader events can be converted to writer events easily, and so it is possible to write XML transformation chains in a pretty clean manner.
This parser is mostly full-featured, however, there are limitations:
- Legacy code pages and non-Unicode encodings are not supported;
- DTD validation is not supported (but entities defined in the internal subset are supported);
- attribute value normalization is not performed, and end-of-line characters are not normalized either.
Other than that the parser tries to be mostly XML-1.1-compliant.
Writer is also mostly full-featured with the following limitations:
- no support for encodings other than UTF-8,
- no support for emitting
<!DOCTYPE>
declarations; - more validations of input are needed, for example, checking that namespace prefixes are bounded or comments are well-formed.
Building and using
xml-rs uses Cargo, so add it with cargo add xml
or modify Cargo.toml
:
[]
= "0.8.16"
The package exposes a single crate called xml
.
Reading XML documents
xml::reader::EventReader
requires a Read
instance to read from. It can be a File
wrapped in BufReader
, or a Vec<u8>
, or a &[u8]
slice.
EventReader
implements IntoIterator
trait, so you can use it in a for
loop directly:
use File;
use BufReader;
use ;
Document parsing can end normally or with an error. Regardless of exact cause, the parsing process will be stopped, and the iterator will terminate normally.
You can also have finer control over when to pull the next event from the parser using its own
next()
method:
match parser.next
Upon the end of the document or an error, the parser will remember the last event and will always
return it in the result of next()
call afterwards. If iterator is used, then it will yield
error or end-of-document event once and will produce None
afterwards.
It is also possible to tweak parsing process a little using xml::reader::ParserConfig
structure.
See its documentation for more information and examples.
You can find a more extensive example of using EventReader
in src/analyze.rs
, which is a
small program (BTW, it is built with cargo build
and can be run after that) which shows various
statistics about specified XML document. It can also be used to check for well-formedness of
XML documents - if a document is not well-formed, this program will exit with an error.
Parsing untrusted inputs
The parser is written in safe Rust subset, so by Rust's guarantees the worst that it can do is to cause a panic.
You can use ParserConfig
to set limits on maximum lenghts of names, attributes, text, entities, etc.
You should also set a maximum document size via io::Read
's take(max)
method.
Writing XML documents
xml-rs also provides a streaming writer much like StAX event writer. With it you can write an
XML document to any Write
implementor.
use io;
use ;
/// A simple demo syntax where "+foo" makes `<foo>`, "-foo" makes `</foo>`
The code example above also demonstrates how to create a writer out of its configuration.
Similar thing also works with EventReader
.
The library provides an XML event building DSL which helps to construct complex events, e.g. ones having namespace definitions. Some examples:
// <a:hello a:param="value" xmlns:a="urn:some:document">
start_element.attr.ns
// <hello b:config="name" xmlns="urn:default:uri">
start_element.attr.default_ns
// <![CDATA[some unescaped text]]>
cdata
Of course, one can create XmlEvent
enum variants directly instead of using the builder DSL.
There are more examples in xml::writer::XmlEvent
documentation.
The writer has multiple configuration options; see EmitterConfig
documentation for more
information.
Bug reports
Please report issues at: https://github.com/kornelski/xml-rs/issues.