Struct rio_xml::RdfXmlParser

source ·
pub struct RdfXmlParser<R: BufRead> { /* private fields */ }
Expand description

A RDF/XML streaming parser.

It implements the TriplesParser trait. It reads the file in streaming. It does not keep data in memory except a stack for handling nested XML tags, and a set of all seen rdf:IDs to detect duplicate ids and fail according to the specification.

Its performances are not optimized yet and hopefully could be significantly enhanced by reducing the number of allocations and copies done by the parser.

Count the number of people using the TriplesParser API without proper error management:

use rio_xml::{RdfXmlParser, RdfXmlError};
use rio_api::parser::TriplesParser;
use rio_api::model::NamedNode;

let file = b"<?xml version=\"1.0\"?>
<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:schema=\"http://schema.org/\">
 <rdf:Description rdf:about=\"http://example.com/foo\">
   <rdf:type rdf:resource=\"http://schema.org/Person\" />
   <schema:name>Foo</schema:name>
 </rdf:Description>
 <schema:Person rdf:about=\"http://example.com/bar\" schema:name=\"Bar\" />
</rdf:RDF>";

let rdf_type = NamedNode { iri: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" };
let schema_person = NamedNode { iri: "http://schema.org/Person" };
let mut count = 0;
RdfXmlParser::new(file.as_ref(), None).parse_all(&mut |t| {
    if t.predicate == rdf_type && t.object == schema_person.into() {
        count += 1;
    }
    Ok(()) as Result<(), RdfXmlError>
})?;
assert_eq!(2, count);

Implementations§

source§

impl<R: BufRead> RdfXmlParser<R>

source

pub fn new(reader: R, base_iri: Option<Iri<String>>) -> Self

Builds the parser from a BufRead implementation, and a base IRI for relative IRI resolution.

source

pub fn buffer_position(&self) -> usize

The current byte position in the input data.

Trait Implementations§

source§

impl<R: BufRead> TriplesParser for RdfXmlParser<R>

§

type Error = RdfXmlError

source§

fn parse_step<E: From<RdfXmlError>>( &mut self, on_triple: &mut impl FnMut(Triple<'_>) -> Result<(), E> ) -> Result<(), E>

Parses a small chunk of the file and calls on_triple each time a new triple is read. (A “small chunk” could be a line for an N-Triples parser.) Read more
source§

fn is_end(&self) -> bool

Returns true if the file has been completely consumed by the parser.
source§

fn parse_all<E>( &mut self, on_triple: &mut impl FnMut(Triple<'_>) -> Result<(), E> ) -> Result<(), E>where E: From<Self::Error>,

Parses the complete file and calls on_triple each time a new triple is read. Read more
source§

fn into_iter<T, E, F>( self, convert_triple: F ) -> TriplesParserIterator<T, E, F, Self>where E: From<Self::Error>, F: FnMut(Triple<'_>) -> Result<T, E>,

Converts the parser into a Result<T, E> iterator. Read more

Auto Trait Implementations§

§

impl<R> RefUnwindSafe for RdfXmlParser<R>where R: RefUnwindSafe,

§

impl<R> Send for RdfXmlParser<R>where R: Send,

§

impl<R> Sync for RdfXmlParser<R>where R: Sync,

§

impl<R> Unpin for RdfXmlParser<R>where R: Unpin,

§

impl<R> UnwindSafe for RdfXmlParser<R>where R: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.