Rust
Rust
Find XML Element where Attribute = Value
See more XML Examples
Finds an XML element at a particular location where an attribute has a specified value.Chilkat Rust Downloads
// We have the following XML and wish to find the value of the time attribute where msg = "alert-from".
// <cdr id="4e5d3e7f41bf6201ad81000c29703270" e164="6207">
// <user>
// <grp name="wfln" mode="active"/>
// </user>
// <event msg="setup-to" time="287069" e164="5034"/>
// <event msg="alert-from" time="287069" e164="5034"/>
// <event msg="rel-to" time="287079" e164="5034"/>
// </cdr>
// First, build the above XML:
let xml = chilkat::Xml::new();
xml.set_tag("cdr");
let _ = xml.add_attribute("id", "4e5d3e7f41bf6201ad81000c29703270");
let _ = xml.add_attribute("e164", "6207");
let _ = xml.update_attr_at("user|grp", true, "name", "wfln");
let _ = xml.update_attr_at("user|grp", true, "mode", "active");
let _ = xml.update_attr_at("event", true, "msg", "setup-to");
let _ = xml.update_attr_at("event", true, "time", "287069");
let _ = xml.update_attr_at("event", true, "e164", "5034");
let _ = xml.update_attr_at("event[1]", true, "msg", "alert-from");
let _ = xml.update_attr_at("event[1]", true, "time", "287069");
let _ = xml.update_attr_at("event[1]", true, "e164", "5034");
let _ = xml.update_attr_at("event[2]", true, "msg", "rel-to");
let _ = xml.update_attr_at("event[2]", true, "time", "287079");
let _ = xml.update_attr_at("event[2]", true, "e164", "5034");
// Show that we have the above XML
println!("{}", xml.get_xml().unwrap_or_default());
// Find the element where msg = "alert-from"
// This updates the xml object's reference to the found element (if successful).
let Ok(not_used) = xml.chilkat_path("/A/event,msg,alert-from|$") else {
println!("Not found.");
return;
};
// Get the value of the "time" attribute.
println!("time = {}", xml.get_attr_value("time").unwrap_or_default());
// Restore the xml object's internal reference to the root of the XML document
xml.get_root2();