| 1 | # xml5ever |
| 2 | |
| 3 |   |
| 4 | [](https://docs.rs/xml5ever) |
| 5 | [](https://crates.io/crates/xml5ever) |
| 6 | |
| 7 | [API documentation](https://docs.rs/xml5ever) |
| 8 | |
| 9 | **Warning:** This library is alpha quality, so no guarantees are given. |
| 10 | |
| 11 | This crate provides a push based XML parser library that trades well-formedness for error recovery. |
| 12 | |
| 13 | xml5ever is based largely on [html5ever](https://github.com/servo/html5ever) parser, so if you have experience with html5ever you will be familiar with xml5ever. |
| 14 | |
| 15 | The library is dual licensed under MIT and Apache license. |
| 16 | |
| 17 | # Why you should use xml5ever |
| 18 | |
| 19 | Main use case for this library is when XML is badly formatted, usually from bad XML |
| 20 | templates. XML5 tries to handle most common errors, in a manner similar to HTML5. |
| 21 | |
| 22 | ## When you should use it? |
| 23 | |
| 24 | - You aren't interested in well-formed documents. |
| 25 | - You need to get some info from your data even if it has errors (although not all possible errors are handled). |
| 26 | - You want to features like character references or XML namespaces. |
| 27 | |
| 28 | ## When you shouldn't use it |
| 29 | |
| 30 | - You need to have your document validated. |
| 31 | - You require DTD support. |
| 32 | - You require an easy to use parser, with lots of extensions (e.g. XPath, XQuery). |
| 33 | - You require a battle tested, industry proven solution. |
| 34 | |
| 35 | # Installation |
| 36 | |
| 37 | Add xml5ever as a dependency: |
| 38 | |
| 39 | ```bash |
| 40 | cargo add xml5ever |
| 41 | ``` |
| 42 | |
| 43 | # Getting started |
| 44 | |
| 45 | Here is a very simple RcDom backed parser: |
| 46 | |
| 47 | ```rust |
| 48 | |
| 49 | let input = "<xml></xml>".to_tendril(); |
| 50 | |
| 51 | // To parse XML into a tree form, we need a TreeSink |
| 52 | // luckily xml5ever comes with a static RC backed tree represetation. |
| 53 | let dom: RcDom = parse(std::iter::once(input), Default::default()); |
| 54 | |
| 55 | // Do something with dom |
| 56 | |
| 57 | ``` |
| 58 | 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). |
| 59 | |
| 60 | # Working on xml5ever |
| 61 | |
| 62 | To build examples and tests you need to do something along the lines of: |
| 63 | |
| 64 | ```rust |
| 65 | git submodule update --init # to fetch xml5lib-tests |
| 66 | cargo build |
| 67 | cargo test |
| 68 | ``` |
| 69 | |
| 70 | This will fetch tests from outside repository and it will invoke cargo to |
| 71 | build and test the crate. If you need docs checkout either [API docs](https://docs.rs/xml5ever) or run `cargo docs` |
| 72 | to generate documentation. |