anyxml
anyxml
is a fully spec-conformant XML library.
Features
The current implementation supports the following features:
- parse XML 1.0 document
- DTD parsing
- Entity reference substitution (both general entity and parameter entity are supported)
- Character reference substitution
- Attribute value normalization
- Default attribute value handling
- validate XML 1.0 document using DTD
- handle namespace conforming to XML Namespace 1.0
Parser
You can use a SAX-like API designed with reference to Java SAX API.
The key difference from the Java API is that SAX handlers are provided solely as two traits: SAXHandler
and EntityResolver
.
This approach reduces opportunities to use Rc
/Arc
or internal mutability.
Example
use Write as _;
use ;
let mut reader = new
.set_handler
.build;
reader.parse_str.ok;
let handler = reader.handler;
assert_eq!;
Parser (Progressive)
SAX-like parsers appear to retrieve all data from a specific source at once, but in some cases, applications may want to provide data incrementally.
This crate also supports such functionality, which libxml2 calls as "Push type parser" or "Progressive type parser".
In this crate, this feature is called the "Progressive Parser".
This is because "push" and "pull" are generally used as terms to classify how the parser delivers the parsing results to the application, rather than how the source is provided to the parser.
When parsing XML documents that retrieve external resources, note that the application must set the appropriate base URI for the parser before starting parsing.
By default, the current directory is set as the base URI.
Example
use Write as _;
use ;
let mut reader = new
.set_handler
.progressive_parser
.build;
let source = br#"<greeting>Hello!!</greeting>"#;
for chunk in source.chunks
// Note that the last chunk must set `finish` to `true`.
// As shown below, it's okay for an empty chunk.
reader.parse_chunk.ok;
let handler = reader.handler;
assert_eq!;
Conformance
This crate conforms to the following specifications:
Tests
This crate passes the following tests:
- XML Conformance Test Suites
- some self-made tests