diet-xml 0.2.2

Probably the simplest, most approachable XML builder for Rust
Documentation
use diet_xml::XmlBuilder;

fn main() {


    // example data
    let employees = vec![
        Employee { id: 1, first: "John", last: "Doe", dob: "1900-01-01" },
        Employee { id: 2, first: "Jane", last: "Doe", dob: "1800-12-31" },
        Employee { id: 3, first: "John", last: "Dough", dob: "1700-01-01" },
     
    ];

    // create an XmlBuilder struct

    // this is an struct that allows you build an xml output in memory
    let mut xb = XmlBuilder::new();
    
    // define the schema you wish to populate in plain text
    xb.set_schema("
    <root>
        <employee>
            <name>
                <first></first>
                <last></last>
            </name>
            <info>
                <dob></dob>
            </info>
        </employee>
        <passing_str_ok></passing_str_ok>
        <passing_i32_ok></passing_i32_ok>
        <name!2></name!2>   
    </root>");

    // default header is <?xml version="1.0" encoding="UTF-8"?>
    // to add custom headers, multiple allowed use
    // xb.set_header("your customer header, < > will be applied the the star/end automatically");

    for e in employees{

     
        // set element used to group to parent elements
        xb.set_key("employee", e.id)
            .attribute("id", e.id);

          // unlike other libraries the parent elements are implicitly built
        xb.add_element("first", e.first);
        xb.add_element("last", e.last);

        // you can chain into the cdata() method to enclose an element in cdata tags
        xb.add_element("dob", e.dob).cdata();

        // clears current key context
        xb.clear_keys();       
   }


    // values can be passed as ny type implementing to_string()
    xb.add_element("passing_str_ok", "some str");
    xb.add_element("passing_i32_ok", 111222333);

    // duplicate element names should be distinguised by !AnyAlphaNumtext in the schema definition
    xb.add_element("name!2", "suffix !2 has been removed, this enables use of duplicate element names");

   // builds the xml in the background
   xb.build_xml();


    // function .xml_out() returns string of the xml output
    println!("{}", xb.xml_out());
}




struct Employee {
    id: usize,
    first: &'static str,
    last: &'static str,
    dob: &'static str,
}