Crate edit_xml

Source
Expand description

A tree-like parser to read, modify and write XML files.

It was especially designed for modifying xml files without rigid structure.

Parsing from various encodings are supported, including UTF-16, ISO 8859-1, GBK and EUC-KR. (With the notable exception of UTF-32)

The XML document is represented with Document, Element and Node.

§Example

use edit_xml::{Document, Element, Node};

const data: &'static str = r#"<?xml version="1.0" encoding="utf-8"?>
<metadata>     
    <title>The Felloship of the Ring</title>
    <author>J. R. R. Tolkien</author>
    <date>1954</date>
</metadata>
"#;

let mut doc = Document::parse_str(data).unwrap();
let metadata = doc.root_element().unwrap();

// Add a new element
let series = Element::build("series")
    .add_text("Lord of the Rings")
    .push_to(&mut doc, metadata);

// Modify existing element
let date = metadata.find(&doc, "date").unwrap();
date.set_text_content(&mut doc, "29 July 1954");

let xml = doc.write_str();

Below example goes through the root element’s children and removes all nodes that isn’t <conf>...</conf>

use std::path::Path;
use edit_xml::{Document, Node};

let xml_file = Path::new("config.xml");
let mut doc = Document::parse_file(&xml_file).unwrap();
let root = doc.root_element().unwrap();
let to_remove: Vec<usize> = root.children(&doc)
    .iter()
    .enumerate()
    .filter_map(|(i, node)| {
        if let Node::Element(elem) = node {
            if elem.name(&doc) == "conf" {
                return None
            }
        }
        Some(i)
    })
    .collect();
for i in to_remove.iter().rev() {
    root.remove_child(&mut doc, *i);
}
doc.write_file(&xml_file);

Re-exports§

pub use quick_xml;

Modules§

types
utils

Structs§

Document
Represents a XML document or a document fragment.
Element
Represents an XML element. It acts as a pointer to actual element data stored in Document.
ElementBuilder
An easy way to build a new element by chaining methods to add properties.
ElementDebug
Debug implementation for Element
ReadOptions
Options when parsing xml.
WriteOptions
Options when writing XML.

Enums§

DecodeError
EditXMLError
Error types
MalformedReason
Node
Represents an XML node.
NodeDebug

Functions§

normalize_space
#xD(\r), #xA(\n), #x9(\t) is normalized into #x20. Leading and trailing spaces(#x20) are discarded and sequence of spaces are replaced by a single space.

Type Aliases§

Result
Wrapper around std::Result