This content is for v2.x version. Switch to the Stable version.
Parsing
XmlDeserializer turns XML input into
an XmlDocument tree. It reads the XML
declaration, normalizes line breaks, resolves namespaces and preserves comments.
From a byte vector or string
Section titled “From a byte vector or string”use draviavemal_xml_rs::XmlDeserializer;
let xml = r#"<note><to>World</to></note>"#;let document = XmlDeserializer::vec_to_xml_doc_tree(xml.as_bytes().to_vec())?;vec_to_xml_doc_tree takes a Vec<u8>, so pass your_string.as_bytes().to_vec()
for an in-memory string.
From a file
Section titled “From a file”use draviavemal_xml_rs::XmlDeserializer;
let document = XmlDeserializer::file_to_xml_doc_tree("catalog.xml")?;What is preserved
Section titled “What is preserved”| Input | Handling |
|---|---|
| XML declaration | version and encoding are read onto the document (encoding defaults to utf-8). |
| Namespaces | Prefix → URI mappings are resolved; QNames keep their prefix. |
| Attributes | Kept in source order. |
| Text & mixed content | Unescaped and stored as text nodes. |
| Comments | Preserved as comment nodes and re-emitted on serialize. |
| Self-closing tags | Parsed as empty elements. |
Read the declaration metadata
Section titled “Read the declaration metadata”After parsing, the XML declaration values are available on the document:
let version = document.get_version(); // e.g. "1.0"let encoding = document.get_encoding(); // e.g. "UTF-8"let standalone = document.get_standalone(); // Option<&str>let prolog = document.get_prolog_comments(); // comments before the root- Navigating & querying the parsed tree.
- Building & editing documents from scratch.