quick-xml

Build Status Crate

High performance xml pull reader/writer.

Inspired by xml-rs.

Documentation

Usage

Carto.toml toml [dependencies] quick-xml = "0.1"

rust extern crate quick_xml;

Example

Reader

```rust use quick_xml::{XmlReader, Event};

let xml = r#" Test Test 2 "#; let reader = XmlReader::fromstr(xml).trimtext(true); let mut count = 0; let mut txt = Vec::new(); for r in reader { match r { Ok(Event::Start(ref e)) => { match e.name() { b"tag1" => println!("attributes values: {:?}", e.attributes().map(|a| a.unwrap().1).collect::>()), b"tag2" => count += 1, _ => (), } }, Ok(Event::Text(e)) => txt.push(e.into_string()), Err((e, pos)) => panic!("{:?} at position {}", e, pos), _ => (), } } ```

Writer

```rust use quickxml::{AsStr, Element, Event, XmlReader, XmlWriter}; use quickxml::Event::*; use std::io::Cursor; use std::iter;

let xml = r#"text"#; let reader = XmlReader::fromstr(xml).trimtext(true); let mut writer = XmlWriter::new(Cursor::new(Vec::new())); for r in reader { match r { Ok(Event::Start(ref e)) if e.name() == b"this_tag" => { // collect existing attributes let mut attrs = e.attributes().map(|attr| attr.unwrap()).collect::>();

        // copy existing attributes, adds a new my-key="some value" attribute
        let mut elem = Element::new("my_elem").with_attributes(attrs);
        elem.push_attribute(b"my-key", "some value");

        // writes the event to the writer
        assert!(writer.write(Start(elem)).is_ok());
    },
    Ok(Event::End(ref e)) if e.name() == b"this_tag" => {
        assert!(writer.write(End(Element::new("my_elem"))).is_ok());
    },
    Ok(e) => assert!(writer.write(e).is_ok()),
    Err((e, pos)) => panic!("{:?} at position {}", e, pos),
}

}

let result = writer.intoinner().intoinner(); let expected = r#"text"#; asserteq!(result, expected.asbytes()); ```

Current state

quick-xml has been written to be fast.

On my first tests (200mb+ xmls) it performs much better (minimum 10x) than xml-rs.

While this is a still WIP and only basic xml specifications are implemented, you can already use it for simple enough xmls (no namespaces, no exotic characters etc ...).

This is particularly true when the xml is generated by a known source (e.g. OpenStreetMap).

Todo

There are many xml specifications not implemented yet: - [ ] namespaces - [ ] non-utf8 - [ ] parse xml prologue - [ ] ~~XQueries~~ - [ ] more checks - [ ] ... and many other things I don't even know!

Contribute

Any PR is welcomed!

License

MIT