Skip to main content

XmlParser

Struct XmlParser 

Source
pub struct XmlParser;
Expand description

A utility for parsing XML content.

This struct provides helper methods for common XML parsing operations used when extracting data from EPUB files (OPF, NCX, and HTML content).

Implementations§

Source§

impl XmlParser

Source

pub fn extract_text<R: BufRead>( reader: &mut Reader<R>, buf: &mut Vec<u8>, ) -> Result<Option<String>, Box<dyn Error>>

Extracts text content from an XML reader.

Reads events from the XML reader until a text event is found or the element ends. This is useful for extracting the text content of XML elements like <title>, <creator>, etc.

§Arguments
  • reader - The XML reader to read events from.
  • buf - A buffer for reading events (will be cleared automatically).
§Returns

Returns Ok(Some(String)) if text was found, Ok(None) if no text was found before the element ended, or an error if parsing fails.

§Errors

Returns an error if there is an XML parsing error.

§Example
use quick_xml::Reader;
use epub_parser::utils::XmlParser;

let xml = r#"<title>My Book</title>"#;
let mut reader = Reader::from_str(xml);
let mut buf = Vec::new();

// Skip the Start event
reader.read_event_into(&mut buf).unwrap();

let text = XmlParser::extract_text(&mut reader, &mut buf).unwrap();
assert_eq!(text, Some("My Book".to_string()));

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

Source§

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.