Getting Started
xml_rs is published on crates.io as draviavemal-xml_rs and imported in
code as draviavemal_xml_rs.
| Resource | Link |
|---|---|
| Crate | crates.io/crates/draviavemal-xml_rs |
| API docs | docs.rs/draviavemal-xml_rs |
| Source & issues | github.com/DraviaVemal/xml_rs |
Requirements
- Rust (2021 edition)
- UTF-8 encoded XML input
Install
-
Add the crate to your project:
Terminal window cargo add draviavemal-xml_rs -
Import the types you need:
use draviavemal_xml_rs::{XmlDeserializer, XmlSerializer, XmlDocument, XmlAttribute, XmlElementContentType,};
Parse, read and serialize
The three building blocks are
XmlDeserializer (XML → DOM),
XmlDocument (the tree) and
XmlSerializer (DOM → XML).
use draviavemal_xml_rs::{XmlDeserializer, XmlSerializer};
fn main() -> anyhow::Result<()> { let xml = r#"<catalog xmlns:t="http://example.org/test"> <book id="bk101"><title>XML Basics</title></book> </catalog>"#;
// Parse bytes into a document tree. let mut document = XmlDeserializer::vec_to_xml_doc_tree(xml.as_bytes().to_vec())?;
// Read the root element. let root_id = document.get_root_id(); let root = document.get_element(root_id)?; println!("root tag: {}", root.get_tag()); // "catalog"
// Serialize back to XML bytes (round-trip safe). let bytes = XmlSerializer::xml_tree_to_vec(&mut document)?; println!("{}", String::from_utf8_lossy(&bytes)); Ok(())}Read from / write to files
use draviavemal_xml_rs::{XmlDeserializer, XmlSerializer};
let mut document = XmlDeserializer::file_to_xml_doc_tree("input.xml")?;XmlSerializer::xml_doc_tree_to_file(&mut document, "output.xml")?;Where to next
- Parsing — turn XML into a document tree.
- Navigating & querying — walk the DOM and find nodes.
- Building & editing — construct and modify documents.
- API Reference — the full public surface.