4 releases (2 breaking)

0.3.0 Mar 8, 2020
0.2.0 Mar 8, 2020
0.1.1 Apr 3, 2015
0.1.0 Apr 1, 2015

#2237 in Parser implementations

Download history 19007/week @ 2024-02-05 19531/week @ 2024-02-12 17625/week @ 2024-02-19 18704/week @ 2024-02-26 19058/week @ 2024-03-04 20965/week @ 2024-03-11 24004/week @ 2024-03-18 19340/week @ 2024-03-25 22780/week @ 2024-04-01 28690/week @ 2024-04-08 30848/week @ 2024-04-15 31024/week @ 2024-04-22 31690/week @ 2024-04-29 27227/week @ 2024-05-06 28608/week @ 2024-05-13 31066/week @ 2024-05-20

44,087 downloads per month
Used in 73 crates (16 directly)

MIT/Apache

57KB
1.5K SLoC

RustyXML

Build Status

Documentation

RustyXML is a namespace aware XML parser written in Rust. Right now it provides a basic SAX-like API, and an ElementBuilder based on that.

The parser itself is derived from OFXMLParser as found in ObjFW https://webkeks.org/objfw/.

The current limitations are:

  • Incomplete error checking
  • Unstable API

This project tracks Rust stable.

Examples

Parse a string into an Element struct:

use xml::Element;

let elem: Option<Element> = "<a href='//example.com'/>".parse();

Get events from parsing string data:

use xml::{Event, Parser};

// Create a new Parser
let mut p = Parser::new();

// Feed data to be parsed
p.feed_str("<a href");
p.feed_str("='//example.com'/>");

// Get events for the fed data
for event in p {
    match event.unwrap() {
        Event::ElementStart(tag) => println!("<{}>", tag.name),
        Event::ElementEnd(tag) => println!("</{}>", tag.name),
        _ => ()
    }
}

This should print:

<a>
</a>

Build Elements from Parser Events:

use xml::{Parser, ElementBuilder};

let mut p = xml::Parser::new();
let mut e = xml::ElementBuilder::new();

p.feed_str("<a href='//example.com'/>");
for elem in p.filter_map(|x| e.handle_event(x)) {
    match elem {
        Ok(e) => println!("{}", e),
        Err(e) => println!("{}", e),
    }
}

Build Elements by hand:

let mut reply = xml::Element::new("iq".into(), Some("jabber:client".into()),
                                  vec![("type".into(), None, "error".into()),
                                       ("id".into(), None, "42".into())]);
reply.tag(xml::Element::new("error".into(), Some("jabber:client".into()),
                            vec![("type".into(), None, "cancel".into())]))
     .tag_stay(xml::Element::new("forbidden".into(),
                                 Some("urn:ietf:params:xml:ns:xmpp-stanzas".into()),
                                 vec![]))
     .tag(xml::Element::new("text".into(),
                            Some("urn:ietf:params:xml:ns:xmpp-stanzas".into()),
                            vec![]))
     .text("Permission denied".into());

Result (some whitespace added for readability):

<iq xmlns='jabber:client' id='42' type='error'>
  <error type='cancel'>
    <forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
    <text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>Permission denied</text>
  </error>
</iq>

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps

Features