31 releases (18 breaking)

0.20.0 Sep 11, 2024
0.18.1 Jun 5, 2024
0.18.0 Mar 22, 2024
0.17.0 Mar 27, 2022
0.1.0 Nov 30, 2015

#419 in Parser implementations

Download history 27872/week @ 2024-07-28 26861/week @ 2024-08-04 38089/week @ 2024-08-11 31344/week @ 2024-08-18 40126/week @ 2024-08-25 35057/week @ 2024-09-01 35775/week @ 2024-09-08 28848/week @ 2024-09-15 34416/week @ 2024-09-22 41435/week @ 2024-09-29 25938/week @ 2024-10-06 37388/week @ 2024-10-13 31232/week @ 2024-10-20 35642/week @ 2024-10-27 27734/week @ 2024-11-03 51653/week @ 2024-11-10

149,291 downloads per month
Used in 277 crates (8 directly)

MIT/Apache

230KB
5.5K SLoC

xml5ever

http://www.apache.org/licenses/LICENSE-2.0 https://opensource.org/licenses/MIT Docs.rs

API documentation

Warning: This library is alpha quality, so no guarantees are given.

This crate provides a push based XML parser library that trades well-formedness for error recovery.

xml5ever is based largely on html5ever parser, so if you have experience with html5ever you will be familiar with xml5ever.

The library is dual licensed under MIT and Apache license.

Why you should use xml5ever

Main use case for this library is when XML is badly formatted, usually from bad XML templates. XML5 tries to handle most common errors, in a manner similar to HTML5.

When you should use it?

  • You aren't interested in well-formed documents.
  • You need to get some info from your data even if it has errors (although not all possible errors are handled).
  • You want to features like character references or XML namespaces.

When you shouldn't use it

  • You need to have your document validated.
  • You require DTD support.
  • You require an easy to use parser, with lots of extensions (e.g. XPath, XQuery).
  • You require a battle tested, industry proven solution.

Installation

Add xml5ever as a dependency in your project manifest:

    [dependencies]
    xml5ever = "0.18"

Getting started

Here is a very simple RcDom backed parser:


    let input = "<xml></xml>".to_tendril();

    // To parse XML into a tree form, we need a TreeSink
    // luckily xml5ever comes with a static RC backed tree represetation.
    let dom: RcDom = parse(std::iter::once(input), Default::default());

    // Do something with dom

The thing that does actual parsing is the parse function. It expects an iterator that can be converted into StrTendril, so you can use std::iter::once(input) or Some(input).into_iter() (where input is StrTendril like structure).

Working on xml5ever

To build examples and tests you need to do something along the lines of:

    git submodule update --init # to fetch xml5lib-tests
    cargo build
    cargo test

This will fetch tests from outside repository and it will invoke cargo to build and test the crate. If you need docs checkout either API docs or run cargo docs to generate documentation.

Dependencies

~0.7–5.5MB
~21K SLoC