quick_xml is way faster than xml-rs
Here is an example with a quick atom parser:
With xml-rs:
test parse_factorio_atom ... bench: 3,295,678 ns/iter (+/- 165,851)
With quick_xml:
test parse_factorio_atom ... bench: 203,215 ns/iter (+/- 13,485)
Unfortunately I had to break the API for this change to happen.
* Element::from_reader now takes `R: BufRead` instead of `R: Read`
* Element::write_to now takes `W: io::Write` instead of `EventWriter<W: Write>`
This migration also allow us to have a write_to function which assumes
we're already in a given namespace (see `write_to_in_namespace`).
78 lines
2 KiB
Rust
78 lines
2 KiB
Rust
#![deny(missing_docs)]
|
|
|
|
//! A minimal DOM crate built on top of xml-rs.
|
|
//!
|
|
//! This library exports an `Element` struct which represents a DOM tree.
|
|
//!
|
|
//! # Example
|
|
//!
|
|
//! Run with `cargo run --example articles`. Located in `examples/articles.rs`.
|
|
//!
|
|
//! ```rust,ignore
|
|
//! extern crate minidom;
|
|
//!
|
|
//! use minidom::Element;
|
|
//!
|
|
//! const DATA: &'static str = r#"<articles xmlns="article">
|
|
//! <article>
|
|
//! <title>10 Terrible Bugs You Would NEVER Believe Happened</title>
|
|
//! <body>
|
|
//! Rust fixed them all. <3
|
|
//! </body>
|
|
//! </article>
|
|
//! <article>
|
|
//! <title>BREAKING NEWS: Physical Bug Jumps Out Of Programmer's Screen</title>
|
|
//! <body>
|
|
//! Just kidding!
|
|
//! </body>
|
|
//! </article>
|
|
//! </articles>"#;
|
|
//!
|
|
//! const ARTICLE_NS: &'static str = "article";
|
|
//!
|
|
//! #[derive(Debug)]
|
|
//! pub struct Article {
|
|
//! title: String,
|
|
//! body: String,
|
|
//! }
|
|
//!
|
|
//! fn main() {
|
|
//! let root: Element = DATA.parse().unwrap();
|
|
//!
|
|
//! let mut articles: Vec<Article> = Vec::new();
|
|
//!
|
|
//! for child in root.children() {
|
|
//! if child.is("article", ARTICLE_NS) {
|
|
//! let title = child.get_child("title", ARTICLE_NS).unwrap().text();
|
|
//! let body = child.get_child("body", ARTICLE_NS).unwrap().text();
|
|
//! articles.push(Article {
|
|
//! title: title,
|
|
//! body: body.trim().to_owned(),
|
|
//! });
|
|
//! }
|
|
//! }
|
|
//!
|
|
//! println!("{:?}", articles);
|
|
//! }
|
|
//! ```
|
|
//!
|
|
//! # Usage
|
|
//!
|
|
//! To use `minidom`, add this to your `Cargo.toml` under `dependencies`:
|
|
//!
|
|
//! ```toml,ignore
|
|
//! minidom = "*"
|
|
//! ```
|
|
|
|
extern crate quick_xml;
|
|
#[macro_use] extern crate error_chain;
|
|
|
|
pub mod error;
|
|
pub mod element;
|
|
pub mod convert;
|
|
|
|
#[cfg(test)] mod tests;
|
|
|
|
pub use error::{Error, ErrorKind, Result, ResultExt};
|
|
pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder};
|
|
pub use convert::{IntoElements, IntoAttributeValue, ElementEmitter};
|