| 2 | |
| 3 | const NS: &str = ""; |
| 4 | fn main() -> Result<(), Box<dyn std::error::Error + 'static>> { |
| 5 | println!("minidom"); |
| 6 | let data = std::fs::read_to_string("../../cd-list.xml")?; |
| 7 | let mut root: Element = data.parse().unwrap(); |
| 8 | |
| 9 | for child in root.children() { |
| 10 | println!("{:?}", child); |
| 11 | } |
| 12 | |
| 13 | for child in root.children().filter(|e| e.is("title", NS)) { |
| 14 | println!("title {:?}", child.text()); |
| 15 | } |
| 16 | for child in root.children().filter(|e| e.is("cd", NS)) { |
| 17 | for tl in child.children().filter(|e| e.is("track-list", NS)) { |
| 18 | for track in tl.children().filter(|e| e.is("track", NS)) { |
| 19 | println!("track {:?}", track.attr("no")); |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | let newcd = Element::builder("cd", "") |
| 25 | .append( |
| 26 | Element::builder("artist", "") |
| 27 | .append("bob") |
| 28 | .attr("country", "DE"), |
| 29 | ) |
| 30 | .append(Element::builder("title", "").append("Song for Alice")) |
| 31 | .build(); |
| 32 | root.append_child(newcd); |
| 33 | let mut buf = Vec::<u8>::new(); |
| 34 | root.write_to(&mut buf).unwrap(); |
| 35 | println!("{}", String::from_utf8(buf).unwrap()); |
| 36 | Ok(()) |
| 37 | } |