document all the things!
This commit is contained in:
parent
e0b35477cd
commit
0a45a6993e
4 changed files with 334 additions and 5 deletions
45
examples/articles.rs
Normal file
45
examples/articles.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
extern crate minidom;
|
||||
|
||||
use minidom::Element;
|
||||
|
||||
const DATA: &'static str = r#"<articles xmlns="article">
|
||||
<article>
|
||||
<title>10 Terrible Bugs You Would NEVER Believe Happened</title>
|
||||
<body>
|
||||
Rust fixed them all. <3
|
||||
</body>
|
||||
</article>
|
||||
<article>
|
||||
<title>BREAKING NEWS: Physical Bug Jumps Out Of Programmer's Screen</title>
|
||||
<body>
|
||||
Just kidding!
|
||||
</body>
|
||||
</article>
|
||||
</articles>"#;
|
||||
|
||||
const ARTICLE_NS: &'static str = "article";
|
||||
|
||||
#[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);
|
||||
}
|
||||
Loading…
Reference in a new issue