Sample code for 30+ languages & platforms
Rust

Auto-Trim XML Content when Loading

See more XML Examples

This example explains the "autoTrim" argument that is passed to a method such as LoadXml2.

Chilkat Rust Downloads

Rust

let xml = chilkat::Xml::new();

// If autoTrim is true, then the content inside an leaf element is trimmed.
// For example:
let mut auto_trim = true;
let _ = xml.load_xml2("<abc><xyz>  123   </xyz></abc>", auto_trim);
println!("{}", xml.get_xml().unwrap_or_default());

// Output is:
// (notice the SPACE chars before and after "xyz" are trimmed)

// <?xml version="1.0" encoding="utf-8" ?>
// <abc>
//     <xyz>123</xyz>
// </abc>

// If autoTrim is false, then the content inside leaf elements are not trimmed.
auto_trim = false;
let _ = xml.load_xml2("<abc><xyz>  123   </xyz></abc>", auto_trim);
println!("{}", xml.get_xml().unwrap_or_default());

// Output is:

// <?xml version="1.0" encoding="utf-8" ?>
// <abc>
//     <xyz>  123   </xyz>
// </abc>

// --------------------------------------------------------------------
// The EmitCompact property controls whether XML is emitted indented (pretty-printed)
// or compact.  For example:

// Auto-trim + emit compact:
auto_trim = true;
let _ = xml.load_xml2("<abc><xyz>  123   </xyz></abc>", auto_trim);
xml.set_emit_compact(true);
println!("{}", xml.get_xml().unwrap_or_default());

// Output is:

// <?xml version="1.0" encoding="utf-8" ?>
// <abc><xyz>123</xyz></abc>

// No Auto-trim + emit compact:
auto_trim = false;
let _ = xml.load_xml2("<abc><xyz>  123   </xyz></abc>", auto_trim);
xml.set_emit_compact(true);
println!("{}", xml.get_xml().unwrap_or_default());

// Output is:

// <?xml version="1.0" encoding="utf-8" ?>
// <abc><xyz>  123   </xyz></abc>