Crate xml_dom

A Rust crate providing a reasonably faithful implementation of the W3C Document Object Model Core, Level 2.

MIT License Minimum Rust Version crates.io docs.rs Build Audit GitHub stars

This crate provides a trait-based implementation of the DOM with minimal changes to the style and semantics defined in the Level 2 specification. The specific mapping from the IDL in the specification is described in the documentation, however from a purely style point of view the implementation has the following characteristics:

  1. It maintains a reasonable separation between the node type traits and the tree implementation using opaque an RefNode reference type.
  2. Where possible the names from IDL are used with minimal conversion.
  3. All IDL attributes become trait functions (attribute "foo" becomes foo(), set_foo(), and unset_foo()).

This leads to a replication of the typical DOM programmer experience where casting between the node traits is required. This is supported by the xml_dom::convert module.

Example

```rust use xmldom::level2::*; use xmldom::level2::convert::*;

// Bootstrap; get an instance of DOMImplementation. The mechanism for this is // intentionally undefined by the specification. let implementation = get_implementation();

// Create a DocumentType instance. let documenttype = implementation .createdocument_type( "html", Some("-//W3C//DTD XHTML 1.0 Transitional//EN"), Some("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"), ) .unwrap();

// Create a new Document using the document type defined above. Note that this // also has the side-effect of creating the document's root element named "html". let mut documentnode = implementation .createdocument(Some("http://www.w3.org/1999/xhtml"), Some("html"), Some(document_type)) .unwrap();

// Cast the returned document RefNode into a RefDocument trait reference let document = asdocumentmut(&mut document_node).unwrap();

// Fetch the document's root element as a node, then cast to RefElement. let mut rootnode = document.documentelement().unwrap(); let root = aselementmut(&mut root_node).unwrap();

// Create an Attribute instance on the root element. root.set_attribute("lang", "en");

// Create two child Elements of "html". let head = root.appendchild(document.createelement("head").unwrap()); let _body = root.appendchild(document.create_element("body").unwrap());

// Display as XML. let xml = documentnode.tostring(); println!("document 2: {}", xml); ```

This should result in the following XML; note that formatting was added for this document, the provided implementation of Display for RefNode does not format the output.

xml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en"> <head></head> <body></body> </html>

Features

Currently only one feature, quick_parser, is provided which provides a new module parser with the single public function. This feature is enabled by default.

rust pub fn read_xml(xml: &str) -> Result<RefNode>;

This will parse the document and return a new RefNode that corresponds to the Document trait.

Changes

Version 0.2.5

Version 0.2.4

Version 0.2.3

Version 0.2.2

Version 0.2.1

Version 0.2.0

Version 0.1.4

Version 0.1.3

Version 0.1.2

Version 0.1.1

Version 0.1.0

TODO

  1. More tests required.
  2. More doc test/examples.
  3. Ensure correct notation replacement.