Crate xml_dom
A Rust crate providing a reasonably faithful implementation of the W3C Document Object Model Core, Level 2.
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:
- It maintains a reasonable separation between the node type traits and the
tree implementation using opaque an
RefNodereference type. - Where possible the names from IDL are used with minimal conversion.
- All IDL attributes become trait functions (attribute "foo" becomes
foo(),set_foo(), andunset_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
use *;
use *;
// 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 document_type = implementation
.create_document_type
.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 document_node = implementation
.create_document
.unwrap;
// Cast the returned document `RefNode` into a `RefDocument` trait reference
let document = as_document_mut.unwrap;
// Fetch the document's root element as a node, then cast to `RefElement`.
let mut root_node = document.document_element.unwrap;
let root = as_element_mut.unwrap;
// Create an `Attribute` instance on the root element.
root.set_attribute;
// Create two child `Element`s of "html".
let _head = root.append_child;
let _body = root.append_child;
// Display as XML.
let xml = document_node.to_string;
println!;
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.
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.
;
This will parse the document and return a new RefNode that corresponds to the
Document trait.
Changes
Version 0.2.8
- Dependency management:
- Updated
quick-xmlwhich had some breaking API changes. - Moved from
logcrate totracing. - Removed
this_errordependency.
- Updated
Version 0.2.7
- Updated to 2021 Edition of Rust
- Updated quick-xml dependency.
- Refactored
Errortype with thiserror- Encapsulated errors from dependant libraries and removed manual
Fromimplementations. - Removed unused
Errorenum types.
- Encapsulated errors from dependant libraries and removed manual
- Made several interfaces more generic with
AsRef<str>andInto<String>.
Version 0.2.6
- Updated quick-xml dependency.
- Had to update the handling of errors from the underlying parser.
- Had to handle the CData parser explicitly.
Version 0.2.5
- Added
parser::from_readerfunction alongside the existingparser::from_strto allow for streaming input of the underlying source.
Version 0.2.4
- Updated quick-xml dependency.
- Moved TODOs into GitHub issues.
- Started migration to GitHub actions for build.
Version 0.2.3
- Mostly documentation updates/fixes.
- Made namespace support respect
ProcessingOptions.
Version 0.2.2
- Mostly documentation updates/fixes.
- Fixed a defect in handling of
xml:id.
Version 0.2.1
- Bug Fixes:
- Fixed a publishing error in Travis
- Separated
Attribute::owner_elementfromNode::parent_node, they aren't the same. - Fixed handling of
owner_elementinAttributetests - Fixed the implementation of
WrongDocumenterror inNode::insert_beforeand used the same inElement::set_attribute_node. - Fixed escaping of values to happen on get not set.
- Implemented attribute value normalization and end-of-line handling from the
XML 1.1 spec.
- Required added a dependency on
regex - Added
EntityResolvertrait for callback into DOM. - Expansion of
Entitynode children is not yet complete.
- Required added a dependency on
- Code clean-up:
- Added some macros in
trait_implsfor a number of common patterns. - Simplified some existing
matchexpressions.
- Added some macros in
Version 0.2.0
- Cleaned up documentation.
- Stable release.
Version 0.1.4
- BREAKING refactored to add a
level2module, allowing other levels to be added at a later time. Also moved extensions intolevel2::extmodule. - BREAKING renamed methods to conform with DOM names:
Node::nametoNode::node_name;CharacterData::substringtoCharacterData::substring_data;CharacterData::appendtoCharacterData::append_data;CharacterData::inserttoCharacterData::insert_data;CharacterData::deletetoCharacterData::delete_data;CharacterData::replacetoCharacterData::replace_data.
- Implemented the following methods:
Node::clone_node;Node::normalize;Namespaced::normalize_mappings.
- Added the following DOM methods:
Attribute::owner_element;Node::local_name;Node::namespace_uri;Node::prefix.
- CI builds now working with Travis, rust-xml_dom.
- Added
quick_xmlbased text parser. - Make this the 0.2.0 candidate.
Version 0.1.3
- More unit tests overall, especially for append/insert/replace child
- Add support for xml declaration (
XmlDecl,XmlVersion), not reusing processing instruction - Support the last Level 2 extended interfaces (
Entity,EntityReference, andNotation).- Also, add
create_notation,create_entity, andcreate_internal_entitytodom_impl.
- Also, add
- Implement an options (
ProcessingOptionsandDOMImplementation::create_document_with_options) capability to turn on extended processing behaviors. - Fixed some nested borrow issues.
Version 0.1.2
- Focus on feature completion:
- implement all core trait features
- implement extended trait features for currently supported traits
- unescaping text
- refactor
NodeImplfor extended traits
- Unit tests, lot's of unit tests
Version 0.1.1
- Focus on API, separate the traits from implementation more cleanly.
- Better
Displayformatting - Better
append_childrule support - Have support for namespace resolution
- Have support for text escaping on setting values
- More examples, fleshing out more of the common methods.
- Note, this is NOT YET ready for production usage.
Version 0.1.0
- Focus on modeling as traits, not all methods actually implemented.
- Note, this is NOT YET ready for production usage.