Sample code for 30+ languages & platforms
Rust

XML SearchForTag Method

See more XML Examples

Demonstrates the SearchForTag and SearchForTag2 methods.

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

<root>
    <searchRoot>
        <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>
    </searchRoot>
    <fruit color="red">strawberry</fruit>
    <fruit color="orange">peach</fruit>
</root>

Chilkat Rust Downloads

Rust
let mut success = false;

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

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

// Search the sub-tree rooted at "searchRoot";
let x_search_root = xml.find_child("searchRoot").unwrap();

// Search for all nodes having the tag "fruit";
let mut x_begin_after = x_search_root.get_self().unwrap();
let mut x_found = x_search_root.search_for_tag(Some(&x_search_root), "fruit").unwrap();
while x_search_root.last_method_success() {

    println!("{}", x_found.content());

    x_begin_after = x_found;
    x_found = x_search_root.search_for_tag(Some(&x_begin_after), "fruit").unwrap();
}

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

// ---------------------------------------------------------------------------------
// Now do the same, but instead use SearchForTag2
// which updates the internal reference of the caller instead
// of returning the found node.

x_begin_after = x_search_root.get_self().unwrap();
let x_search = x_search_root.get_self().unwrap();

success = x_search.search_for_tag2(Some(&x_begin_after), "fruit").is_ok();
while success {

    println!("{}", x_search.content());

    // Copy the internal references so that the next search
    // begins after the found node.
    x_begin_after.copy_ref(&x_search);
    x_search.copy_ref(&x_search_root);

    success = x_search.search_for_tag2(Some(&x_begin_after), "fruit").is_ok();
}