Sample code for 30+ languages & platforms
Rust

Methods for Getting Attributes

See more XML Examples

Demonstrates some methods for getting attribute name/values.

The input XML, available at http://www.chilkatsoft.com/data/car.xml, is this:

<root>
    <car color="black" make="mercedes" model="C350" hp="302" engine="v6" type="sedan">Mercedes Benz C350</car>
</root>

Chilkat Rust Downloads

Rust

let xml = chilkat::Xml::new();
let mut i: i32 = 0;

// The sample input XML is available at http://www.chilkatsoft.com/data/car.xml
if xml.load_xml_file("car.xml").is_err() {
    println!("{}", xml.last_error_text());
    return;
}

// Navigate to the "car" node, which is the 1st child:
let car_node = xml.first_child().unwrap();

// Get the value of the "model" attribute:
println!("model = {}", car_node.get_attr_value("model").unwrap_or_default());

// Get the value of the "hp" attribute as an integer:
let horsepower = car_node.get_attr_value_int("hp");
println!("horsepower = {}", horsepower);

// Iterate over the attributes and show the name/value of each:
let num_attr = car_node.num_attributes();

i = 0;
while i < num_attr {
    println!("{}: {}", car_node.get_attribute_name(i).unwrap_or_default(), car_node.get_attribute_value(i).unwrap_or_default());
    i = i + 1;
}