articles.rs

 1// Copyright (c) 2020 lumi <lumi@pew.im>
 2//
 3// This Source Code Form is subject to the terms of the Mozilla Public
 4// License, v. 2.0. If a copy of the MPL was not distributed with this
 5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7extern crate minidom;
 8
 9use minidom::Element;
10
11const DATA: &str = r#"<articles xmlns="article">
12    <article>
13        <title>10 Terrible Bugs You Would NEVER Believe Happened</title>
14        <body>
15            Rust fixed them all. &lt;3
16        </body>
17    </article>
18    <article>
19        <title>BREAKING NEWS: Physical Bug Jumps Out Of Programmer's Screen</title>
20        <body>
21            Just kidding!
22        </body>
23    </article>
24</articles>"#;
25
26const ARTICLE_NS: &str = "article";
27
28#[derive(Debug)]
29pub struct Article {
30    pub title: String,
31    pub body: String,
32}
33
34fn main() {
35    let root: Element = DATA.parse().unwrap();
36
37    let mut articles: Vec<Article> = Vec::new();
38
39    for child in root.children() {
40        if child.is("article", ARTICLE_NS) {
41            let title = child.get_child("title", ARTICLE_NS).unwrap().text();
42            let body = child.get_child("body", ARTICLE_NS).unwrap().text();
43            articles.push(Article {
44                title: title,
45                body: body.trim().to_owned(),
46            });
47        }
48    }
49
50    println!("{:?}", articles);
51}