xmpp-rs-mirror/examples/articles.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2017-02-21 14:46:06 +00:00
extern crate minidom;
use minidom::Element;
2017-11-12 19:58:07 +00:00
const DATA: &str = r#"<articles xmlns="article">
2017-02-21 14:46:06 +00:00
<article>
<title>10 Terrible Bugs You Would NEVER Believe Happened</title>
<body>
Rust fixed them all. &lt;3
</body>
</article>
<article>
<title>BREAKING NEWS: Physical Bug Jumps Out Of Programmer's Screen</title>
<body>
Just kidding!
</body>
</article>
</articles>"#;
2017-11-12 19:58:07 +00:00
const ARTICLE_NS: &str = "article";
2017-02-21 14:46:06 +00:00
#[derive(Debug)]
pub struct Article {
title: String,
body: String,
}
fn main() {
let root: Element = DATA.parse().unwrap();
let mut articles: Vec<Article> = Vec::new();
for child in root.children() {
if child.is("article", ARTICLE_NS) {
let title = child.get_child("title", ARTICLE_NS).unwrap().text();
let body = child.get_child("body", ARTICLE_NS).unwrap().text();
articles.push(Article {
title: title,
body: body.trim().to_owned(),
});
}
}
println!("{:?}", articles);
}