Crate diet_xml

Source
Expand description

§diet-xml

A schema-driven, ergonomic XML builder for Rust.

§Example

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 a 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 remove all headers use xb.clear_header();
    // to set customer header use xb.set_header("your customer header, < > will be applied the the star/end automatically");

    for e in employees{

        // we want each id to relate to it's own employee element
        // so use the id as the key 
        // we can also chain into attributes to have the id display as an element attribute
       
        // these arguments can be anything that implments .to_string()
        // eg String, usize, &str, int
        // (element_the_key_is_on, key_value) 
        // this is to allow the other to quickly add values to elements
        // without needing boiler plate to convert to text where possible

        xb.set_key("employee", e.id)
            .attribute("id", e.id);

        // you can chain either an add_element or set_key into .attribute
        // this takes two arguments
        // these arguments can be anything that implments .to_string()
        // eg String, usize, &str, int
        // (attribute_name, attribute_value)
        // the .attributes() method can be used to add multiple attributes
        // this should be passed in the form
        // &[(&str,&str),(&str,&str),(&str,&str),(&str,&str)]
        // tuples pairs of &str with attribute name and value
        // attribute and attributes can be chained from add_element also

        // now we simply add the element values we want populated
        // there is no need to build the entire structure
        // 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 keys resets all context to a default value
        // this isn't necessary in this sample program
        // as keys are all overwritten on each iteration
        // keys retain their state until either overwritten or cleared
        xb.clear_keys();       
   }


    // passing any type that implements to_string as a value for
    // add_element, attribute(), set_key is ok
    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
    // the suffix will be remove when the XML is produced
    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,
}

Structs§

ChainFromAdd
XmlBuilder