articles.rs

 1extern crate minidom;
 2
 3use minidom::Element;
 4
 5const DATA: &str = r#"<articles xmlns="article">
 6    <article>
 7        <title>10 Terrible Bugs You Would NEVER Believe Happened</title>
 8        <body>
 9            Rust fixed them all. &lt;3
10        </body>
11    </article>
12    <article>
13        <title>BREAKING NEWS: Physical Bug Jumps Out Of Programmer's Screen</title>
14        <body>
15            Just kidding!
16        </body>
17    </article>
18</articles>"#;
19
20const ARTICLE_NS: &str = "article";
21
22#[derive(Debug)]
23pub struct Article {
24    title: String,
25    body: String,
26}
27
28fn main() {
29    let root: Element = DATA.parse().unwrap();
30
31    let mut articles: Vec<Article> = Vec::new();
32
33    for child in root.children() {
34        if child.is("article", ARTICLE_NS) {
35            let title = child.get_child("title", ARTICLE_NS).unwrap().text();
36            let body = child.get_child("body", ARTICLE_NS).unwrap().text();
37            articles.push(Article {
38                title: title,
39                body: body.trim().to_owned(),
40            });
41        }
42    }
43
44    println!("{:?}", articles);
45}