Enum nom_xml::Document

source ·
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

Fields

§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)

Implementations§

source§

impl Document

source

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>>>)>

source

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?
examples/extract_information.rs (line 128)
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(())
}
source

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?
examples/parse_first_matching_element.rs (line 30)
27
28
29
30
31
32
33
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)?;
    println!("{doc:?}");
    Ok(())
}
More examples
Hide additional examples
examples/parse_element_with_specific_attribute_value.rs (lines 29-33)
26
27
28
29
30
31
32
33
34
35
36
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",
        &Some(vec![Attribute::new("isbn", "978-0316332910")]),
    )?;
    println!("{doc:?}");
    Ok(())
}
examples/extract_information.rs (line 162)
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(())
}
source

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

Examples found in repository?
examples/parse_all_of_specific_tag.rs (line 31)
27
28
29
30
31
32
33
34
fn main() -> Result<()> {
    let mut file = File::open("examples/TheExpanseSeries.xml")?;
    let data = read_file(&mut file)?;

    let (_, doc) = Document::parse_elements_by_tag_name(&data, "book", &None)?;
    println!("{doc:?}");
    Ok(())
}

Trait Implementations§

source§

impl Clone for Document

source§

fn clone(&self) -> Document

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Document

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl DynamicEquality for Document

source§

fn equals(&self, pattern: Pattern<'_>, method: ComparisonMethod) -> bool

source§

impl<'a> Parse<'a> for Document

source§

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:?}");
§

type Args = Config

§

type Output = Result<(&'a str, Document), Err<Error>>

source§

fn is_char(c: char) -> bool

source§

fn parse_char(input: &str) -> IResult<&str, char>

source§

fn is_whitespace(c: char) -> bool

source§

fn parse_multispace1(input: &str) -> IResult<&str, ()>

source§

fn parse_multispace0(input: &str) -> IResult<&str, ()>

source§

fn is_name_start_char(c: char) -> bool

source§

fn is_name_char(c: char) -> bool

source§

fn parse_name_char(input: &str) -> IResult<&str, char>

source§

fn parse_name_start_char(input: &str) -> IResult<&str, char>

source§

fn parse_nmtoken(input: &str) -> IResult<&str, String>

source§

fn parse_nmtokens(input: &str) -> IResult<&str, Vec<String>>

source§

fn parse_name(input: &str) -> IResult<&str, Name>

source§

fn parse_names(input: &str) -> IResult<&str, Vec<Name>>

source§

fn parse_eq(input: &str) -> IResult<&str, ()>

source§

fn capture_span<O, F>( f: F ) -> Box<dyn FnMut(&'a str) -> IResult<&'a str, (&'a str, O)> + 'a>
where F: FnMut(&'a str) -> IResult<&'a str, O> + 'a,

source§

impl<'a> ParseNamespace<'a> for Document

source§

impl PartialEq for Document

source§

fn eq(&self, other: &Document) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Document

source§

impl StructuralPartialEq for Document

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.