Rust, Software Development

Rust – Generate EPCIS 1.2 XML With Sample Codes

Processing a considerable number of large XML files may require a lot of memory and processing power. With Rust, we can create applications that process huge XML files with a relatively small memory footprint yet are still performant. This post shows how to generate simple EPCIS 1.2 XML using Rust.

Rust Requirements To Write Out EPCIS 1.2 XML

We used the following items for this post.

  • Rust 1.52.1
  • Cargo
  • Rust Crates
    • xml-rs 0.8.3
      • For writing XML files
    • uuid 0.8.2
      • For generating unique identifiers
    • chrono 0.4.19
      • For date/time-related elements
  • Intellij IDEA
  • Rust plugin for Intellij IDEA

Target EPCIS 1.2 XML Output

We aim to create the following EPCIS 1.2 XML using Rust and some crates.

Writing out XML using Closure

This post also demonstrates how effective the solution described in A Convenient Way to Write Out Start and End XML Tags using Closure is, especially for large XML data.

Update Rust [crayon-62bb606db917b731957696-i/] With Dependencies

After creating a project in IntelliJ IDEA, we update the cargo.toml as follows.

Create Rust Codes To Write Out EPCIS 1.2 XML File

First, we create the and import the stuff we need.

Then, create a helper function that converts String to &str. We need this function for some string conversion because some functions from xml-rs expect some parameters of type &str instead of String.

Then, we create our “convenient” function that writes out an XML element as a block. It uses a closure to allow for nested XML element blocks, i.e., a block within a block.

Then, we create another function that calls the write_element_block  to build up that EPCIS 1.2 XML content.

Finally, we code the function main function as follows.

When we run this Rust program, it first creates an XML file. Then, it makes a writer so our functions handle_event can use to write out each XML block building up the target EPCIS 1.2 XML file.

The codes generate the following output on the console.


Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like