Module amxml::sax[][src]

Subset of simple SAX processor.

Module sax implements a subset of simple XML parser.

Examples

Basic usage:

  • Create the SaxDecoder object with the XML string as argument.

  • Call SaxDecoder#raw_token() in loop. raw_token() returns the next token, which is one of StartElement, EndElement, CharData, etc.

  • If XML string has standalone element like <foo/>, raw_token() returns in 2 phases: first as StartElement <foo>, and second as EndElement </foo>.

  • SaxDecoder decodes the entity/charcter references, e.g., '&gt;' into '>'.

use amxml::sax::*;
let xml_string = r#"<?xml version="1.0"?><!--COM--><root a="v">-&gt;text&#169;<img/></root>"#;
let mut dec = SaxDecoder::new(&xml_string).unwrap();
let mut buf = String::from("");
loop {
    match dec.raw_token() {
        Ok(XmlToken::EOF) => {
            buf += "EOF";
            break;
        },
        Ok(XmlToken::StartElement{name, attr}) => {
            buf += &format!("[S] {}; ", name);
            for at in attr.iter() {
                buf += &format!("[A] {} = \"{}\"; ", at.name(), at.value());
            }
        },
        Ok(XmlToken::EndElement{name}) => {
            buf += &format!("[E] {}; ", name);
        },
        Ok(XmlToken::CharData{chardata}) => {
            buf += &format!("[T] \"{}\"; ", chardata);
        },
        Ok(XmlToken::ProcInst{target, inst}) => {
            buf += &format!("[P] {}: {}; ", target, inst);
        },
        Ok(XmlToken::Comment{comment}) => {
            buf += &format!("[C] {}; ", comment);
        },
        _ => {},
    }
}
assert_eq!(buf, r#"[P] xml: version="1.0"; [C] COM; [S] root; [A] a = "v"; [T] "->text©"; [S] img; [E] img; [E] root; EOF"#);

Note

SaxDecoder does not verify that StartElement and EndElement match. Also SaxDecoder does not translate namespace prefixes to their corresponding URIs.

SaxDecoder does not care Directives <!DOCTYPE ...>, <!ELEMENT ...>, etc.

SaxDecoder recognizes the XML declaration as ProcInit. Caller should check if target equals to "xml".

SaxDecoder accepts some illegal XML documents, like those that have more than one XML declarations, more than one root elements.

Structs

Attr

In XmlToken::StartElement, attribute of element.

SaxDecoder

SaxDecoder represents an XML parser reading a particular input stream. See the module document for details.

Enums

XmlToken

XmlToken, return type of SaxDecoder#raw_token()