MCPcopy Create free account
hub / github.com/bedroombuilds/python2rust / main

Function main

1c_xml/rust/xml/src/main.rs:3–36  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1use xmltree::{Element, XMLNode};
2
3fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
4 println!("Hello, world!");
5 let data = std::fs::read_to_string("../../cd-list.xml")?;
6 let mut root = Element::parse(data.as_bytes()).unwrap();
7 for child in &root.children {
8 println!("child {:?}", child);
9 }
10 let titles = &root.get_child("title").unwrap();
11 for title in &titles.children {
12 println!("title {:?}", title.as_text());
13 println!("title {:?}", &titles.get_text());
14 }
15 let cd = &root.get_child("cd").unwrap();
16 let track_list = cd.get_child("track-list").unwrap();
17 let track = track_list.get_child("track").unwrap();
18 println!("track-no {:?}", track.attributes["no"]);
19
20 let mut newcd = Element::new("cd");
21 let mut artist = Element::new("artist");
22 artist.children.push(XMLNode::Text("Bob".to_owned()));
23 artist
24 .attributes
25 .insert("country".to_owned(), "DE".to_owned());
26 let mut title = Element::new("title");
27 title
28 .children
29 .push(XMLNode::Text("Song for Alice".to_owned()));
30 newcd.children.push(XMLNode::Element(title));
31 root.children.push(XMLNode::Element(newcd));
32 let mut buf = Vec::<u8>::new();
33 root.write(&mut buf).unwrap();
34 println!("{}", String::from_utf8(buf).unwrap());
35 Ok(())
36}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected