Sample code for 30+ languages & platforms
Rust

Traverse Direct Children via FirstChild / NextSibling, or LastChild / PreviousSibling

See more XML Examples

Demonstrates some ways to iterate over direct child nodes using the FirstChild / NextSibling and LastChild / PreviousSibling methods.

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

<root>
    <fruit color="red">apple</fruit>
    <fruit color="green">pear</fruit>
    <veg color="orange">carrot</veg>
    <meat animal="cow">beef</meat>
    <xyz>
        <fruit color="blue">blueberry</fruit>
        <veg color="green">broccoli</veg>
    </xyz>
    <fruit color="purple">grape</fruit>
    <cheese color="yellow">cheddar</cheese>
</root>

Chilkat Rust Downloads

Rust
let mut success = false;

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

success = xml.load_xml_file("qa_data/xml/fruit.xml").is_ok();
if !success {
    println!("{}", xml.last_error_text());
    return;
}

// Iterate over the direct children by using FirstChild / NextSibling
let mut child = xml.first_child().unwrap();
let mut b_continue = xml.last_method_success();
while b_continue {
    println!("{} : {}", child.tag(), child.content());
    let next_sibling = child.next_sibling().unwrap();
    b_continue = child.last_method_success();

    child = next_sibling;
}

println!("-----");

// Do the same, but with FirstChild2 / NextSibling2 to avoid
// creating so many XML object instances:
success = xml.first_child2().is_ok();
while success {
    println!("{} : {}", xml.tag(), xml.content());
    success = xml.next_sibling2().is_ok();
}

// Revert back up to the parent:
success = xml.get_parent2().is_ok();

println!("-----");

// Iterate in reverse order using LastChild / PreviousSibling
child = xml.last_child().unwrap();
b_continue = xml.last_method_success();
while b_continue {
    println!("{} : {}", child.tag(), child.content());
    let prev_sibling = child.previous_sibling().unwrap();
    b_continue = child.last_method_success();

    child = prev_sibling;
}

println!("-----");

// Do the same, but with LastChild2 / PreviousSibling2 to avoid
// creating so many XML object instances:
success = xml.last_child2().is_ok();
while success {
    println!("{} : {}", xml.tag(), xml.content());
    success = xml.previous_sibling2().is_ok();
}

// Revert back up to the parent:
success = xml.get_parent2().is_ok();