Crates.io | xml_dom |
lib.rs | xml_dom |
version | 0.2.8 |
source | src |
created_at | 2020-03-06 22:31:17.554502 |
updated_at | 2024-07-22 21:46:52.255187 |
description | A Rust crate providing a reasonably faithful implementation of the W3C DOM Core |
homepage | |
repository | https://github.com/johnstonskj/rust-xml_dom.git |
max_upload_size | |
id | 216193 |
size | 413,170 |
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:
RefNode
reference type.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.
use xml_dom::level2::*;
use xml_dom::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 document_type = implementation
.create_document_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 document_node = implementation
.create_document(
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 = as_document_mut(&mut document_node).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(&mut root_node).unwrap();
// Create an `Attribute` instance on the root element.
root.set_attribute("lang", "en");
// Create two child `Element`s of "html".
let _head = root.append_child(document.create_element("head").unwrap());
let _body = root.append_child(document.create_element("body").unwrap());
// Display as XML.
let xml = document_node.to_string();
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.
<!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>
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.
pub fn read_xml(xml: AsRef<str>) -> Result<RefNode>;
This will parse the document and return a new RefNode
that corresponds to the
Document
trait.
quick-xml
which had some breaking API changes.log
crate to tracing
.this_error
dependency.Error
type with thiserror
From
implementations.Error
enum types.AsRef<str>
and Into<String>
.parser::from_reader
function alongside the existing parser::from_str
to
allow for streaming input of the underlying source.ProcessingOptions
.xml:id
.Attribute::owner_element
from Node::parent_node
, they aren't the
same.owner_element
in Attribute
testsWrongDocument
error in Node::insert_before
and
used the same in Element::set_attribute_node
.regex
EntityResolver
trait for callback into DOM.Entity
node children is not yet complete.trait_impls
for a number of common patterns.match
expressions.level2
module, allowing other levels to be added
at a later time. Also moved extensions into level2::ext
module.Node::name
to Node::node_name
;CharacterData::substring
to CharacterData::substring_data
;CharacterData::append
to CharacterData::append_data
;CharacterData::insert
to CharacterData::insert_data
;CharacterData::delete
to CharacterData::delete_data
;CharacterData::replace
to CharacterData::replace_data
.Node::clone_node
;Node::normalize
;Namespaced::normalize_mappings
.Attribute::owner_element
;Node::local_name
;Node::namespace_uri
;Node::prefix
.quick_xml
based text parser.XmlDecl
, XmlVersion
), not reusing processing
instructionEntity
, EntityReference
, and
Notation
).
create_notation
, create_entity
, and create_internal_entity
to
dom_impl
.ProcessingOptions
and
DOMImplementation::create_document_with_options
) capability to turn on
extended processing behaviors.NodeImpl
for extended traitsDisplay
formattingappend_child
rule support