xml::reader

Enum XmlEvent

Source
pub enum XmlEvent {
    StartDocument {
        version: XmlVersion,
        encoding: String,
        standalone: Option<bool>,
    },
    EndDocument,
    ProcessingInstruction {
        name: String,
        data: Option<String>,
    },
    StartElement {
        name: OwnedName,
        attributes: Vec<OwnedAttribute>,
        namespace: Namespace,
    },
    EndElement {
        name: OwnedName,
    },
    CData(String),
    Comment(String),
    Characters(String),
    Whitespace(String),
}
Expand description

An element of an XML input stream.

Items of this enum are emitted by reader::EventReader. They correspond to different elements of an XML document.

Variants§

§

StartDocument

Corresponds to XML document declaration.

This event is always emitted before any other event. It is emitted even if the actual declaration is not present in the document.

Fields

§version: XmlVersion

XML version.

If XML declaration is not present, defaults to Version10.

§encoding: String

XML document encoding.

If XML declaration is not present or does not contain encoding attribute, defaults to "UTF-8". This field is currently used for no other purpose than informational.

§standalone: Option<bool>

XML standalone declaration.

If XML document is not present or does not contain standalone attribute, defaults to None. This field is currently used for no other purpose than informational.

§

EndDocument

Denotes to the end of the document stream.

This event is always emitted after any other event (except Error). After it is emitted for the first time, it will always be emitted on next event pull attempts.

§

ProcessingInstruction

Denotes an XML processing instruction.

This event contains a processing instruction target (name) and opaque data. It is up to the application to process them.

Fields

§name: String

Processing instruction target.

§data: Option<String>

Processing instruction content.

§

StartElement

Denotes a beginning of an XML element.

This event is emitted after parsing opening tags or after parsing bodiless tags. In the latter case EndElement event immediately follows.

Fields

§name: OwnedName

Qualified name of the element.

§attributes: Vec<OwnedAttribute>

A list of attributes associated with the element.

Currently attributes are not checked for duplicates (TODO)

§namespace: Namespace

Contents of the namespace mapping at this point of the document.

§

EndElement

Denotes an end of an XML element.

This event is emitted after parsing closing tags or after parsing bodiless tags. In the latter case it is emitted immediately after corresponding StartElement event.

Fields

§name: OwnedName

Qualified name of the element.

§

CData(String)

Denotes CDATA content.

This event contains unparsed data. No unescaping will be performed.

It is possible to configure a parser to emit Characters event instead of CData. See pull::ParserConfiguration structure for more information.

§

Comment(String)

Denotes a comment.

It is possible to configure a parser to ignore comments, so this event will never be emitted. See pull::ParserConfiguration structure for more information.

§

Characters(String)

Denotes character data outside of tags.

Contents of this event will always be unescaped, so no entities like &lt; or &amp; or &#123; will appear in it.

It is possible to configure a parser to trim leading and trailing whitespace for this event. See pull::ParserConfiguration structure for more information.

§

Whitespace(String)

Denotes a chunk of whitespace outside of tags.

It is possible to configure a parser to emit Characters event instead of Whitespace. See pull::ParserConfiguration structure for more information. When combined with whitespace trimming, it will eliminate standalone whitespace from the event stream completely.

Implementations§

Source§

impl XmlEvent

Source

pub fn as_writer_event(&self) -> Option<XmlEvent<'_>>

Obtains a writer event from this reader event.

This method is useful for streaming processing of XML documents where the output is also an XML document. With this method it is possible to process some events while passing other events through to the writer unchanged:

use std::str;

use xml::reader::XmlEvent as ReaderEvent;
use xml::writer::XmlEvent as WriterEvent;
use xml::{EventReader, EventWriter};

let mut input: &[u8] = b"<hello>world</hello>";
let mut output: Vec<u8> = Vec::new();

{
    let mut reader = EventReader::new(&mut input);
    let mut writer = EventWriter::new(&mut output);

    for e in reader {
        match e.unwrap() {
            ReaderEvent::Characters(s) =>
                writer.write(WriterEvent::characters(&s.to_uppercase())).unwrap(),
            e => if let Some(e) = e.as_writer_event() {
                writer.write(e).unwrap()
            }
        }
    }
}

assert_eq!(
    str::from_utf8(&output).unwrap(),
    r#"<?xml version="1.0" encoding="UTF-8"?><hello>WORLD</hello>"#
);

Note that this API may change or get additions in future to improve its ergonomics.

Trait Implementations§

Source§

impl Clone for XmlEvent

Source§

fn clone(&self) -> XmlEvent

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 XmlEvent

Source§

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

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

impl PartialEq for XmlEvent

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for XmlEvent

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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

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.