File: tree_example.rs

package info (click to toggle)
rust-libxml 0.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: xml: 239; ansic: 45; makefile: 2
file content (31 lines) | stat: -rw-r--r-- 737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use libxml::parser::Parser;
use libxml::tree::*;

fn my_recurse(node: &Node) {
  match node.get_type().unwrap() {
    NodeType::ElementNode => {
      println!("Entering {}", node.get_name());
    }
    NodeType::TextNode => {
      println!("Text: {}", node.get_content());
    }
    _ => {}
  }

  let mut c: Option<Node> = node.get_first_child();
  while let Some(child) = c {
    my_recurse(&child);
    c = child.get_next_sibling();
  }

  if node.get_type().unwrap() == NodeType::ElementNode {
    println!("Leaving {}", node.get_name());
  }
}

fn main() {
  let parser = Parser::default();
  let doc = parser.parse_file("tests/resources/file01.xml").unwrap();
  let root = doc.get_root_element().unwrap();
  my_recurse(&root);
}