Docs.rs
  • quick-xml-0.37.5
    • quick-xml 0.37.5
    • Permalink
    • Docs.rs crate page
    • MIT
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • tafia
    • Mingun
    • dralley
    • Dependencies
      • arbitrary ^1 normal optional
      • document-features ^0.2 normal optional
      • encoding_rs ^0.8 normal optional
      • memchr ^2.1 normal
      • serde >=1.0.139 normal optional
      • tokio ^1.10 normal optional
      • criterion ^0.4 dev
      • pretty_assertions ^1.4 dev
      • regex ^1 dev
      • serde-value ^0.7 dev
      • serde_derive ^1.0.206 dev
      • tokio ^1.21 dev
      • tokio-test ^0.4 dev
    • Versions
    • 100% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • Rust
    • About docs.rs
    • Privacy policy
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate quick_xml

quick_xml0.37.5

  • All Items

Sections

  • Description
  • Examples
  • Features

Crate Items

  • Re-exports
  • Modules
  • Macros

Crates

  • quick_xml

Crate quick_xml

Source
Expand description

High performance XML reader/writer.

§Description

quick-xml contains two modes of operation:

A streaming API based on the StAX model. This is suited for larger XML documents which cannot completely read into memory at once.

The user has to explicitly ask for the next XML event, similar to a database cursor. This is achieved by the following two structs:

  • Reader: A low level XML pull-reader where buffer allocation/clearing is left to user.
  • Writer: A XML writer. Can be nested with readers if you want to transform XMLs.

Especially for nested XML elements, the user must keep track where (how deep) in the XML document the current event is located.

quick-xml contains optional support of asynchronous reading and writing using tokio. To get it enable the async-tokio feature.

Furthermore, quick-xml also contains optional Serde support to directly serialize and deserialize from structs, without having to deal with the XML events. To get it enable the serialize feature. Read more about mapping Rust types to XML in the documentation of de module. Also check serde_helpers module.

§Examples

  • For a reading example see Reader
  • For a writing example see Writer

§Features

quick-xml supports the following features:

  • async-tokio — Enables support for asynchronous reading and writing from tokio’s IO-Traits by enabling reading events from types implementing tokio::io::AsyncBufRead.

  • encoding — Enables support of non-UTF-8 encoded documents. Encoding will be inferred from the XML declaration if it is found, otherwise UTF-8 is assumed.

    Currently, only ASCII-compatible encodings are supported. For example, UTF-16 will not work (therefore, quick-xml is not standard compliant).

    Thus, quick-xml supports all encodings of encoding_rs except these:

    • UTF-16BE
    • UTF-16LE
    • ISO-2022-JP

    You should stop processing a document when one of these encodings is detected, because generated events can be wrong and do not reflect a real document structure!

    Because these are the only supported encodings that are not ASCII compatible, you can check for them:

    use quick_xml::events::Event;
    use quick_xml::reader::Reader;
    
    let xml = to_utf16le_with_bom(r#"<?xml encoding='UTF-16'><element/>"#);
    let mut reader = Reader::from_reader(xml.as_ref());
    reader.config_mut().trim_text(true);
    
    let mut buf = Vec::new();
    let mut unsupported = false;
    loop {
        if !reader.decoder().encoding().is_ascii_compatible() {
            unsupported = true;
            break;
        }
        buf.clear();
        match reader.read_event_into(&mut buf).unwrap() {
            Event::Eof => break,
            _ => {}
        }
    }
    assert_eq!(unsupported, true);

    This restriction will be eliminated once issue #158 is resolved.

  • escape-html — Enables support for recognizing all HTML 5 entities in unescape function. The full list of entities also can be found in https://html.spec.whatwg.org/entities.json.

  • overlapped-lists — This feature is for the Serde deserializer that enables support for deserializing lists where tags are overlapped with tags that do not correspond to the list.

    When this feature is enabled, the XML:

    <any-name>
      <item/>
      <another-item/>
      <item/>
      <item/>
    </any-name>

    could be deserialized to a struct:

    #[derive(Deserialize)]
    #[serde(rename_all = "kebab-case")]
    struct AnyName {
      item: Vec<()>,
      another_item: (),
    }

    When this feature is not enabled (default), only the first element will be associated with the field, and the deserialized type will report an error (duplicated field) when the deserializer encounters a second <item/>.

    Note, that enabling this feature can lead to high and even unlimited memory consumption, because deserializer needs to check all events up to the end of a container tag (</any-name> in this example) to figure out that there are no more items for a field. If </any-name> or even EOF is not encountered, the parsing will never end which can lead to a denial-of-service (DoS) scenario.

    Having several lists and overlapped elements for them in XML could also lead to quadratic parsing time, because the deserializer must check the list of events as many times as the number of sequence fields present in the schema.

    To reduce negative consequences, always limit the maximum number of events that Deserializer will buffer.

    This feature works only with serialize feature and has no effect if serialize is not enabled.

  • serde-types — Enables serialization of some quick-xml types using serde. This feature is rarely needed.

    This feature does NOT provide XML serializer or deserializer. You should use the serialize feature for that instead.

  • serialize — Enables support for serde serialization and deserialization. When this feature is enabled, quick-xml provides serializer and deserializer for XML.

    This feature does NOT enables serializaton of the types inside quick-xml. If you need that, use the serde-types feature.

Re-exports§

pub use crate::encoding::Decoder;
pub use crate::errors::serialize::DeError;serialize
pub use crate::errors::serialize::SeError;serialize
pub use crate::errors::Error;
pub use crate::errors::Result;
pub use crate::reader::NsReader;
pub use crate::reader::Reader;
pub use crate::writer::ElementWriter;
pub use crate::writer::Writer;

Modules§

deserialize
Serde Deserializer module.
encoding
A module for wrappers that encode / decode data.
errors
Error management module
escape
Manage xml character escapes
events
Defines zero-copy XML events used throughout this library.
name
Module for handling names according to the W3C Namespaces in XML 1.1 (Second Edition) specification
parser
Contains low-level parsers of different XML pieces.
reader
Contains high-level interface for a pull-based XML parser.
seserialize
Module to handle custom serde Serializer
serde_helpersserde-types
Provides helper functions to glue an XML with a serde content model.
writer
Contains high-level interface for an events-based XML emitter.

Macros§

impl_deserialize_for_internally_tagged_enumserde-types
A helper to implement Deserialize for internally tagged enums which does not use Deserializer::deserialize_any that produces wrong results with XML because of serde#1183.

Results

Settings
Help
    struct
    quick_xml::writer::Writer
    XML writer. Writes XML Events to a std::io::Write …
    module
    quick_xml::writer
    Contains high-level interface for an events-based XML …
    struct field
    quick_xml::se::SimpleTypeSerializer::writer
    Writer to which this serializer writes content
    enum
    quick_xml::se::WriteResult
    Classification of the type written by the serializer.
    function
    quick_xml::se::to_writer
    Serialize struct into a Writer.
    function
    quick_xml::se::to_writer_with_root
    Serialize struct into a Writer using specified root tag …
    struct
    quick_xml::writer::ElementWriter
    A struct to write an element. Contains methods to add …
    function
    quick_xml::se::to_utf8_io_writer
    Serialize struct into a io::Writer restricted to utf-8 …
    method
    quick_xml::writer::Writer::into_inner
    Writer<W> -> W
    Consumes this Writer, returning the underlying writer.
    method
    quick_xml::writer::Writer::get_ref
    &Writer<W> -> &W
    Get a reference to the underlying writer.
    method
    quick_xml::writer::Writer::clone
    &Writer<W> -> Writer<W>
    method
    quick_xml::writer::Writer::get_mut
    &mut Writer<W> -> &mut W
    Get a mutable reference to the underlying writer.
    method
    quick_xml::writer::Writer::write_bom
    &mut Writer<W> -> Result<()>
    Write a Byte-Order-Mark character to the document.
    method
    quick_xml::writer::Writer::write_indent
    &mut Writer<W> -> Result<()>
    Manually write a newline and indentation at the proper …
    method
    quick_xml::writer::Writer::write_indent_async
    &mut Writer<W> -> Result<()>
    Manually write a newline and indentation at the proper …
    method
    quick_xml::writer::Writer::write_event
    &mut Writer<W>, E -> Result<()>
    Writes the given event to the underlying writer.
    method
    quick_xml::writer::Writer::create_element
    &mut Writer<W>, N -> ElementWriter<W>
    Provides a simple, high-level API for writing XML elements.
    method
    quick_xml::writer::Writer::write_event_async
    &mut Writer<W>, E -> Result<()>
    Writes the given event to the underlying writer. Async …
    method
    quick_xml::writer::Writer::write_serializable
    &mut Writer<W>, &str, &T -> Result<(), SeError>
    Write an arbitrary serializable type
    method
    quick_xml::writer::Writer::new
    W -> Writer<W>
    Creates a Writer from a generic writer.
    method
    quick_xml::writer::Writer::new_with_indent
    W, u8, usize -> Writer<W>
    Creates a Writer with configured indents from a generic …
    method
    quick_xml::writer::ElementWriter::write_empty
    ElementWriter<W> -> Result<&mut Writer<W>>
    Write an empty (self-closing) tag.
    method
    quick_xml::writer::ElementWriter::write_pi_content
    ElementWriter<W>, BytesPI -> Result<&mut Writer<W>>
    Write a processing instruction <?...?> inside the current …
    method
    quick_xml::writer::ElementWriter::write_text_content
    ElementWriter<W>, BytesText -> Result<&mut Writer<W>>
    Write some text inside the current element.
    method
    quick_xml::writer::ElementWriter::write_cdata_content
    ElementWriter<W>, BytesCData -> Result<&mut Writer<W>>
    Write a CData event <![CDATA[...]]> inside the current …
    method
    quick_xml::writer::ElementWriter::write_inner_content
    ElementWriter<W>, F -> Result<&mut Writer<W>>
    Create a new scope for writing XML inside the current …
    method
    quick_xml::writer::ElementWriter::write_inner_content_async
    ElementWriter<W>, F -> Result<&mut Writer<W>, E>
    Create a new scope for writing XML inside the current …
    method
    quick_xml::writer::Writer::clone
    &Writer<W> -> Writer<W>