0

Rust – Generate EPCIS 1.2 XML

This post generates a simple EPCIS 1.2 XML using Rust.

Requirements

The following were used for this post.

  • Rust 1.38.0
  • Cargo
  • Rust Crates
    • xml-rs 0.8.0
      • For writing XML files
    • uuid 0.8.1
      • For generating unique identifiers
    • chrono 0.4.9
      • FOr date/time-related elements
  • Intellij IDEA
  • Rust plugin for Intellij IDEA

Target EPCIS Output

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

Writing out XML using Closure

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

Cargo.toml

Update cargo.toml as follows.

Main.rs

First, import the stuff we need.

Then, create a helper function that converts String  to &str .

We need this 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 closure to allow for recursive XML element blocks, i.e., a block within a block within a block.

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

Finally, the main function.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer