pub fn parse(doc: &str) -> Result<Dom, String>Expand description
Parses the tag document and returns a Dom structure tree.
§Arguments
doc- tag document
§Errors
- If the document ends in the middle of a tag or double quote.
§Examples
let html = r#"
<body>
<h1 class="h1">Hello</h1>
</body>
}"#;
if let Ok(dom) = parsercher::parse(&html) {
println!("{:#?}", dom);
}output:
Dom {
dom_type: Tag,
tag: Some(
Tag {
name: "root",
attr: None,
terminated: false,
terminator: false,
},
),
text: None,
comment: None,
children: Some(
[
Dom {
dom_type: Tag,
tag: Some(
Tag {
name: "body",
attr: None,
terminated: false,
terminator: false,
},
),
text: None,
comment: None,
children: Some(
[
Dom {
dom_type: Tag,
tag: Some(
Tag {
name: "h1",
attr: Some(
{
"class": "h1",
},
),
terminated: false,
terminator: false,
},
),
text: None,
comment: None,
children: Some(
[
Dom {
dom_type: Text,
tag: None,
text: Some(
Text {
text: "Hello",
},
),
comment: None,
children: None,
},
],
),
},
],
),
},
],
),
}Examples found in repository?
examples/print_dom_tree.rs (line 13)
3fn main() {
4 let html = r#"
5<head>
6 <meta charset="UTF-8">
7 <title>sample</title>
8</head>
9<body>
10 <h1>Hello, world!</h1>
11</body>
12"#;
13 if let Ok(dom) = parsercher::parse(&html) {
14 parsercher::print_dom_tree(&dom);
15 }
16}More examples
examples/search_tag_from_name.rs (line 12)
3fn main() {
4 let doc = r#"
5<body>
6 <h1 class="h1">section1</h1>
7 <h2 class="h2">section2</h2>
8 <h3 class="h3">section3</h3>
9</body>
10"#;
11
12 if let Ok(dom) = parsercher::parse(&doc) {
13 if let Some(tags) = parsercher::search_tag_from_name(&dom, "h2") {
14 println!("{:#?}", tags);
15 }
16 }
17}examples/search_tag.rs (line 13)
4fn main() {
5 let doc = r#"
6<ol>
7 <li class="target">first</li>
8 <li>second</li>
9 <li id="third" class="target">therd</li>
10</ol>
11"#;
12
13 if let Ok(dom) = parsercher::parse(&doc) {
14 let mut needle = Tag::new("li");
15 needle.set_attr("class", "target");
16 if let Some(tags) = parsercher::search_tag(&dom, &needle) {
17 println!("{:#?}", tags);
18 }
19 }
20}examples/xml_sample.rs (line 23)
3fn main() {
4 let xml = r#"
5<?xml version="1.0"?>
6<Order Number="12345" Date="2021-05-07">
7 <Address Type="Shipping">
8 <Name>ABCD</Name>
9 </Address>
10 <Address Type="Billing">
11 <Name>EFGH</Name>
12 </Address>
13 <Items>
14 <Item ProductNumber="67890">
15 <ProductName>desk</ProductName>
16 </Item>
17 <Item ProductNumber="09876">
18 <ProductName>table</ProductName>
19 </Item>
20 </Items>
21</Order>
22"#;
23 if let Ok(dom) = parsercher::parse(&xml) {
24 parsercher::print_dom_tree(&dom);
25 }
26}examples/search_text_from_tag_children.rs (line 13)
4fn main() {
5 let doc = r#"
6<ol>
7 <li class="target">first</li>
8 <li>second</li>
9 <li class="target">therd</li>
10</ol>
11"#;
12
13 if let Ok(dom) = parsercher::parse(&doc) {
14 let mut needle = Tag::new("li");
15 needle.set_attr("class", "target");
16 if let Some(texts) = parsercher::search_text_from_tag_children(&dom, &needle) {
17 assert_eq!(texts.len(), 2);
18 assert_eq!(texts[0], "first".to_string());
19 assert_eq!(texts[1], "therd".to_string());
20 }
21 }
22}examples/parse.rs (line 30)
3fn main() {
4 let html = r#"
5<!DOCTYPE html>
6<html>
7 <head>
8 <meta charset="UTF-8">
9 <title>sample html</title>
10 </head>
11 <body>
12 <h1>Hello, world!</h1>
13
14 <div
15 id="content"></div>
16
17 <ol>
18 <li>first</li>
19 <li>second</li>
20 <li>therd</li>
21 </ol>
22 <!-- All script code becomes one text -->
23<script>
24 let content = document.getElementById('content');
25 content.textContent = 'content';
26</script>
27 </body>
28</html>
29"#;
30 if let Ok(dom) = parsercher::parse(&html) {
31 println!("{:#?}", dom);
32 }
33}Additional examples can be found in: