Crate serde_xml

Crate serde_xml 

Source
Expand description

§serde_xml

A fast, 100% Serde-compatible XML serialization and deserialization library.

§Features

  • Full Serde compatibility for serialization and deserialization
  • Zero-copy parsing where possible
  • Fast XML tokenization using SIMD-accelerated string searching
  • Support for attributes, namespaces, CDATA, comments, and processing instructions
  • Comprehensive error reporting with line/column positions
  • No unsafe code in the public API

§Quick Start

use serde::{Deserialize, Serialize};
use serde_xml::{from_str, to_string};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Person {
    name: String,
    age: u32,
}

// Serialize to XML
let person = Person {
    name: "Alice".to_string(),
    age: 30,
};
let xml = to_string(&person).unwrap();

// Deserialize from XML
let xml = "<Person><name>Alice</name><age>30</age></Person>";
let person: Person = from_str(xml).unwrap();
assert_eq!(person.name, "Alice");
assert_eq!(person.age, 30);

§Nested Structures

use serde::{Deserialize, Serialize};
use serde_xml::from_str;

#[derive(Debug, Deserialize)]
struct Address {
    city: String,
    country: String,
}

#[derive(Debug, Deserialize)]
struct Person {
    name: String,
    address: Address,
}

let xml = r#"
    <Person>
        <name>Bob</name>
        <address>
            <city>New York</city>
            <country>USA</country>
        </address>
    </Person>
"#;

let person: Person = from_str(xml).unwrap();
assert_eq!(person.address.city, "New York");

§Collections

use serde::{Deserialize, Serialize};
use serde_xml::{from_str, to_string};

#[derive(Debug, Serialize, Deserialize)]
struct Library {
    books: Vec<String>,
}

let library = Library {
    books: vec![
        "The Rust Programming Language".to_string(),
        "Programming Rust".to_string(),
    ],
};

let xml = to_string(&library).unwrap();

§Optional Fields

use serde::{Deserialize, Serialize};
use serde_xml::from_str;

#[derive(Debug, Deserialize)]
struct Config {
    name: String,
    description: Option<String>,
}

let xml = "<Config><name>test</name></Config>";
let config: Config = from_str(xml).unwrap();
assert_eq!(config.description, None);

Re-exports§

pub use de::from_bytes;
pub use de::from_str;
pub use de::Deserializer;
pub use error::Error;
pub use error::ErrorKind;
pub use error::Position;
pub use error::Result;
pub use escape::escape;
pub use escape::unescape;
pub use reader::Attribute;
pub use reader::XmlEvent;
pub use reader::XmlReader;
pub use ser::to_string;
pub use ser::to_string_with_root;
pub use ser::to_vec;
pub use ser::to_writer;
pub use ser::Serializer;
pub use writer::IndentConfig;
pub use writer::XmlWriter;

Modules§

de
Serde deserializer for XML.
error
Error types for XML serialization and deserialization.
escape
XML escape and unescape utilities.
reader
Low-level XML reader/tokenizer.
ser
Serde serializer for XML.
writer
Low-level XML writer.