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