xml_doc/
lib.rs

1//! A tree-like parser to read, modify and write XML files.
2//!
3//! It was especially designed for modifying xml files without rigid structure.
4//!
5//! Parsing from various encodings are supported, including UTF-16, ISO 8859-1, GBK and EUC-KR. (With the notable exception of UTF-32)
6//!
7//! The XML document is represented with [`Document`], [`Element`] and [`Node`].
8//!
9//!
10//! # Example
11//! ```
12//! use xml_doc::{Document, Element, Node};
13//!
14//! const data: &'static str = r#"<?xml version="1.0" encoding="utf-8"?>
15//! <metadata>     
16//!     <title>The Felloship of the Ring</title>
17//!     <author>J. R. R. Tolkien</author>
18//!     <date>1954</date>
19//! </metadata>
20//! "#;
21//!
22//! let mut doc = Document::parse_str(data).unwrap();
23//! let metadata = doc.root_element().unwrap();
24//!
25//! // Add a new element
26//! let series = Element::build(&mut doc, "series")
27//!     .text_content("Lord of the Rings")
28//!     .push_to(metadata);
29//!
30//! // Modify existing element
31//! let date = metadata.find(&doc, "date").unwrap();
32//! date.set_text_content(&mut doc, "29 July 1954");
33//!
34//! let xml = doc.write_str();
35//! ```
36//!
37//! Below example goes through the root element's children and removes all nodes that isn't `<conf>...</conf>`
38//! ```no_run
39//! use std::path::Path;
40//! use xml_doc::{Document, Node};
41//!
42//! let xml_file = Path::new("config.xml");
43//! let mut doc = Document::parse_file(&xml_file).unwrap();
44//! let root = doc.root_element().unwrap();
45//! let to_remove: Vec<usize> = root.children(&doc)
46//!     .iter()
47//!     .enumerate()
48//!     .filter_map(|(i, node)| {
49//!         if let Node::Element(elem) = node {
50//!             if elem.name(&doc) == "conf" {
51//!                 return None
52//!             }
53//!         }
54//!         Some(i)
55//!     })
56//!     .collect();
57//! for i in to_remove.iter().rev() {
58//!     root.remove_child(&mut doc, *i);
59//! }
60//! doc.write_file(&xml_file);
61//! ```
62//!
63mod document;
64mod element;
65mod error;
66mod parser;
67
68pub use crate::document::{Document, Node, WriteOptions};
69pub use crate::element::{Element, ElementBuilder};
70pub use crate::error::{Error, Result};
71pub use crate::parser::{normalize_space, ReadOptions};