Module data::xml

source ·
Expand description

A simple XML parser based on quick-xml.

This is basically an interface on top of quick-xml. It provides an API to let the user write callback functions for specific parts of the document, and hides the reader from the user.

Because of its simple nature, it has a set of limitations:

  • It only parses from a byte buffer. If the XML is from a file, the user needs to read the file into a buffer before using the parser.

  • The XML has to be encoded in UTF-8.

Example

struct A { a: String }

let mut aaa: A = A { a: String::new() };
let mut iin = 0;
let mut out = 0;
let mut d = String::new();

let mut p = Parser::new();
p.addBeginHandler("a", |_, _| {
    iin += 1;
    Ok(())
});
p.addEndHandler("a", |_, _| {
    out += 1;
    Ok(())
});
p.addTextHandler("a", |_, t| {
    aaa.a = t.to_owned();
    Ok(())
});
p.addTagHandler("d", |_, t| {
    d = str::from_utf8(t).unwrap().to_owned();
    Ok(())
});
p.parse(r#"
<c>
    <a>aaa</a>
    <b/>
    <d>
        <e/>
    </d>
</c>
"#.as_bytes()).unwrap();
drop(p);
assert_eq!(iin, 1);
assert_eq!(out, 1);
assert_eq!(aaa.a, "aaa".to_owned());
assert_eq!(d, r#"<d>
        <e/>
    </d>"#);

Structs

  • A simple event callback-based XML parser.

Functions

  • Return the value of the attribute attr from the opening tag tag.