Sample code for 30+ languages & platforms
Rust

Serialize / Deserialize Hashtable to/from XML

Demonstrates how to seralize / deserialize a Hashtable to/from XML.

Note: This example requires Chilkat v9.5.0.64 or later.

Chilkat Rust Downloads

Rust
// Note: This example requires Chilkat v9.5.0.64 or later.

// Add some entries to a hashtable.
let hash_tab = chilkat::Hashtable::new();

let _ = hash_tab.add_str("aaa", "111");
let _ = hash_tab.add_str("bbb", "222");
let _ = hash_tab.add_str("ccc", "333");

// Serialize to XML
let sb = chilkat::StringBuilder::new();
let _ = hash_tab.to_xml_sb(&sb);
println!("{}", sb.get_as_string().unwrap_or_default());
println!("---");

// The output is as follows.  Each hash table entry
// is contained in an "e" node.  The entry's key
// is in the "k" node, and the value in the "v" node.

// <?xml version="1.0" encoding="utf8-8"?>
// <hashtable>
// <e><k>aaa</k><v>111</v></e>
// <e><k>bbb</k><v>222</v></e>
// <e><k>ccc</k><v>333</v></e>
// </hashtable>
// 

// Now load (deserialize) into a new hash table.
let hash_tab2 = chilkat::Hashtable::new();
let _ = hash_tab2.add_from_xml_sb(&sb);

// Get the hash table keys, and lookup each (to show 
// that the hash table was correctly deserialized).
// The GetKeys method can return the keys in any order.
let s_table = chilkat::StringTable::new();
let _ = hash_tab2.get_keys(&s_table);

let mut i = 0;
let num_keys = s_table.count();
while i < num_keys {
    let key = s_table.string_at(i).unwrap_or_default();
    println!("{}: {}", key, hash_tab2.lookup_str(&key).unwrap_or_default());
    i = i + 1;
}