pub enum Document {
Prolog {
xml_decl: Option<XmlDecl>,
misc: Option<Vec<Misc>>,
doc_type: Option<DocType>,
},
Element(Tag, Box<Document>, Tag),
Content(Option<String>),
Nested(Vec<Document>),
Empty,
EmptyTag(Tag),
ProcessingInstruction(ProcessingInstruction),
Comment(String),
CDATA(String),
}
Expand description
Main entry point for parsing XML documents
This enum encapsulates all of the top level types that comprise an XML document. The core variant is the Element(Tag,Box<Document>,Tag)
type which allows recursive parsing of nested tags and their content.
Variants§
Prolog
Element(Tag, Box<Document>, Tag)
Content(Option<String>)
Nested(Vec<Document>)
Empty
EmptyTag(Tag)
ProcessingInstruction(ProcessingInstruction)
Comment(String)
CDATA(String)
Implementations§
source§impl Document
impl Document
pub fn parse_prolog( input: &str, entity_references: Rc<RefCell<HashMap<(Name, EntitySource), EntityValue>>>, config: Config ) -> IResult<&'_ str, (Option<Document>, Rc<RefCell<HashMap<(Name, EntitySource), EntityValue>>>)>
sourcepub fn iter_with_depth(&self, max_level: usize) -> DocumentIterator<'_> ⓘ
pub fn iter_with_depth(&self, max_level: usize) -> DocumentIterator<'_> ⓘ
The main interface for exracting content from the Document tree
See the extract_information
example for more information
Examples found in repository?
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
fn update_field(&mut self, tag: &Tag, doc: &Document) {
let field_name = &tag.name.local_part;
if let Some(attributes_vec) = &tag.attributes {
if let Attribute::Instance {
name,
value: AttributeValue::Value(attr_val),
} = attributes_vec.get(0).unwrap()
{
if name.local_part == "isbn" {
self.isbn = attr_val.to_string();
}
}
}
if let Document::Nested(_) = &doc {
doc.iter_with_depth(1).for_each(|record| {
if let Document::Element(tag, inner_doc, _) = record {
self.update_field(tag, inner_doc);
} else {
eprintln!("Unknown field: {record:#?}");
}
});
} else if let Document::Content(Some(value)) = &doc {
match field_name.as_str() {
"author" => {
self.author = value.to_string();
}
"title" => {
self.title = value.to_string();
}
"genre" => {
self.genre = value.to_string();
}
"description" => {
self.description = value.to_string();
}
e => {
eprintln!("Unknown field: {}", e);
}
}
} else {
eprintln!("Content is missing");
}
}
}
fn main() -> Result<()> {
let mut file = File::open("examples/TheExpanseSeries.xml")?;
let data = read_file(&mut file)?;
let (_, doc) = Document::parse_element_by_tag_name(&data, "book", &None)?;
let mut book = Book::default();
doc.iter_with_depth(0)
.filter_map(|record| {
if let Document::Element(tag, inner_doc, _) = record {
Some((tag, inner_doc))
} else {
None
}
})
.for_each(|(tag, inner_doc)| book.update_field(tag, &inner_doc));
println!("{book:#?}");
Ok(())
}
sourcepub fn parse_element_by_tag_name<'a>(
input: &'a str,
tag_name: &'a str,
attributes: &Option<Vec<Attribute>>
) -> IResult<&'a str, Document>
pub fn parse_element_by_tag_name<'a>( input: &'a str, tag_name: &'a str, attributes: &Option<Vec<Attribute>> ) -> IResult<&'a str, Document>
The main interface for parsing the first element that matches criteria
See the parse_first_matching_element
example for more information
Run with cargo run --example parse_first_matching_element
Also see the parse_element_with_specific_attribute_value
example
Run with cargo run --example parse_element_with_specific_attribute_value
Examples found in repository?
More examples
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
fn main() -> Result<()> {
let mut file = File::open("examples/TheExpanseSeries.xml")?;
let data = read_file(&mut file)?;
let (_, doc) = Document::parse_element_by_tag_name(&data, "book", &None)?;
let mut book = Book::default();
doc.iter_with_depth(0)
.filter_map(|record| {
if let Document::Element(tag, inner_doc, _) = record {
Some((tag, inner_doc))
} else {
None
}
})
.for_each(|(tag, inner_doc)| book.update_field(tag, &inner_doc));
println!("{book:#?}");
Ok(())
}
sourcepub fn parse_elements_by_tag_name<'a>(
input: &'a str,
tag_name: &'a str,
attributes: &Option<Vec<Attribute>>
) -> IResult<&'a str, Vec<Document>>
pub fn parse_elements_by_tag_name<'a>( input: &'a str, tag_name: &'a str, attributes: &Option<Vec<Attribute>> ) -> IResult<&'a str, Vec<Document>>
The main interface for parsing many elements with the same tag name
See the parse_all_of_specific_tag
example for more information
Run with cargo run --example parse_all_of_specific_tag
Trait Implementations§
source§impl DynamicEquality for Document
impl DynamicEquality for Document
source§impl<'a> Parse<'a> for Document
impl<'a> Parse<'a> for Document
source§fn parse(input: &'a str, args: Self::Args) -> Self::Output
fn parse(input: &'a str, args: Self::Args) -> Self::Output
use nom_xml::{parse::Parse, config::Config, Document};
let xml = "<root><child>Content</child></root>";
let (_, doc) = Document::parse(xml, Config::default()).unwrap();
println!("{doc:?}");