Struct simple_xml_serialize::XMLElement[][src]

pub struct XMLElement {
    pub name: String,
    pub contents: Option<Vec<XMLElement>>,
    pub text: Option<String>,
    pub attrs: Option<Vec<XMLAttr>>,
    pub raw: Option<String>,
}

The basic type this crate provides. Functions are provided for setting/adding to the fields in this struct. Any manipulation past that is left to the user by accessing the fields directly.

Fields

name: String

The tag for this element node. IE <myelement/>

contents: Option<Vec<XMLElement>>

Nested XMLElements. IE <myelement><nested/></myelement>

text: Option<String>

Plain character data inside of the node. IE <myelement>hello world</myelement>

attrs: Option<Vec<XMLAttr>>

The key/value pairs inside of an element tag. IE <myelement attr1="hello" attr2="world"/>

raw: Option<String>

Allows for writing raw string contents that won’t have special characters replaced. Useful for including already properly formed xml without it having to be an XMLElement. Placed after ‘contents’ and before ‘text’

Implementations

impl XMLElement[src]

pub fn new(name: &str) -> Self[src]

Constructs a new XMLElement with the given name and None for the rest of the fields

Arguments

  • name - A string slice that holds the name of the XML element displayed at the element root

Example

use simple_xml_serialize::XMLElement;
let ele = XMLElement::new("name");
assert_eq!(ele.to_string(), String::from("<name/>"));

pub fn name(self, name: &str) -> Self[src]

Builder pattern function for changing the name of an XMLElement

Arguments

  • name - A string slice that holds the name of the XML element displayed at the element root

Example

let ele = XMLElement::new("name").name("changed");
assert_eq!(ele.to_string(), String::from("<changed/>"));

pub fn set_name(&mut self, name: &str)[src]

Changes the name of an XMLElement

Arguments

  • name - A string slice that holds the name of the XML element displayed at the element root

Example

let mut ele = XMLElement::new("name");
ele.set_name("changed");
assert_eq!(ele.to_string(), String::from("<changed/>"));

pub fn attr(self, attr: &str, attr_val: impl ToString) -> Self[src]

Builder pattern function for adding an attribute to the XMLElement

Arguments

  • attr - A string slice that holds the name of the attribute
  • attr_val - Any type that implements ToString; the value of the attribute

Example

let ele = XMLElement::new("name").attr("my_attr", 1);
assert_eq!(ele.to_string(), String::from(r#"<name my_attr="1"/>"#));

pub fn add_attr(&mut self, attr: &str, attr_val: impl ToString)[src]

Adds an attribute to the XMLElement

Arguments

  • attr - A string slice that holds the name of the attribute
  • attr_val - Any type that implements ToString; the value of the attribute

Example

let mut ele = XMLElement::new("name");
ele.add_attr("my_attr", 1);
assert_eq!(ele.to_string(), String::from(r#"<name my_attr="1"/>"#));

pub fn element(self, new_ele: impl Into<XMLElement>) -> Self[src]

Builder pattern function for adding an element to the contents of this XMLElement

Arguments

  • new_ele - Any type that implements Into<XMLElement>

Example

struct MyPoint {}
impl From<MyPoint> for XMLElement {
    fn from(p: MyPoint) -> Self {
        XMLElement::new("point")
    }
}
let ele = XMLElement::new("name").element(MyPoint{});
assert_eq!(ele.to_string(), String::from("<name><point/></name>"));

pub fn add_element(&mut self, new_ele: impl Into<XMLElement>)[src]

Adds an element to the contents of this XMLElement

Arguments

  • new_ele - Any type that implements Into<XMLElement>

Example

let mut ele = XMLElement::new("name");
ele.add_element(MyPoint{});
assert_eq!(ele.to_string(), String::from("<name><point/></name>"));

pub fn elements<T>(self, new_eles: T) -> Self where
    T: IntoIterator,
    T::Item: Into<XMLElement>, 
[src]

Builder pattern for adding a collection of elements to the contents of this XMLElement

Arguments

  • new_eles - Any collection of Into<XMLElement> as long as that collection implements IntoIterator

Example

let points: Vec<MyPoint> = vec![MyPoint{}, MyPoint{}];
let mut ele = XMLElement::new("name").elements(points);
assert_eq!(ele.to_string(), String::from("<name><point/><point/></name>"));

pub fn add_elements<T>(&mut self, new_eles: T) where
    T: IntoIterator,
    T::Item: Into<XMLElement>, 
[src]

Adds a collection of elements to the contents of this XMLElement

Arguments

  • new_eles - Any collection of Into<XMLElement> as long as that collection implements IntoIterator

Example

let points: Vec<MyPoint> = vec![MyPoint{}, MyPoint{}];
let mut ele = XMLElement::new("name");
ele.add_elements(points);
assert_eq!(ele.to_string(), String::from("<name><point/><point/></name>"));

pub fn add_elements_with_name<T>(&mut self, new_name: &str, new_eles: T) where
    T: IntoIterator,
    T::Item: Into<XMLElement>, 
[src]

Adds a collection of elements to the contents of this XMLElement and changes the element name of each

Arguments

  • new_name - A string slice containing the name to use when adding the elements
  • new_eles - Any collection of Into<XMLElement> as long as that collection implements IntoIterator

Example

let mut ele = XMLElement::new("name");
let points: Vec<MyPoint> = vec![MyPoint{}, MyPoint{}];
ele.add_elements_with_name("p", points);
assert_eq!(ele.to_string(), String::from("<name><p/><p/></name>"));

pub fn raw(self, raw: impl ToString) -> Self[src]

Builder pattern function for adding raw content to the XMLElement. In the ToString implementation for XMLElement, raw content is placed after normal contents and before text.

Arguments

  • raw - Any type that implements ToString; content in the element

Example

let ele = XMLElement::new("name").raw("<raw_content>Some content</raw_content>");
assert_eq!(ele.to_string(), String::from("<name><raw_content>Some content</raw_content></name>"));

pub fn set_raw(&mut self, raw: impl ToString)[src]

Adds raw text to the contents of the XMLElement. In the ToString implementation for XMLElement, raw content is placed after normal contents and before text.

Arguments

  • text - Any type that implements ToString; text in the element

Example

let mut ele = XMLElement::new("name");
ele.set_raw("<raw_content>Some content</raw_content>");
assert_eq!(ele.to_string(), String::from("<name><raw_content>Some content</raw_content></name>"));

pub fn text(self, text: impl ToString) -> Self[src]

Builder pattern function for adding raw text to the contents of the XMLElement. In the ToString implementation for XMLElement, raw text is always placed after all other contents.

Arguments

  • text - Any type that implements ToString; text in the element

Example

let ele = XMLElement::new("name").text("Some content");
assert_eq!(ele.to_string(), String::from("<name>Some content</name>"));

pub fn set_text(&mut self, text: impl ToString)[src]

Adds raw text to the contents of the XMLElement. In the ToString implementation for XMLElement, raw text is always placed after all other contents.

Arguments

  • text - Any type that implements ToString; text in the element

Example

let mut ele = XMLElement::new("name");
ele.set_text("Some content");
assert_eq!(ele.to_string(), String::from("<name>Some content</name>"));

pub fn to_string_pretty(&self, newline: &str, indent: &str) -> String[src]

Returns the string representation of the XMLElement, but with newlines and the given indentation

Arguments

  • indent - A string slice containing the characters to use to indent the document

Example

let mut ele = XMLElement::new("name");
ele.set_text("Some content");
ele.add_element(MyPoint{});
let expected = String::from(r#"<name>
  <point/>
  Some content
</name>"#);
assert_eq!(ele.to_string_pretty("\n", "  "), expected);

pub fn to_string_pretty_prolog(&self, newline: &str, indent: &str) -> String[src]

Returns the string representation of the XMLElement, but with newlines and the given indentation and an xml prolog

Arguments

  • indent - A string slice containing the characters to use to indent the document

Example

let mut ele = XMLElement::new("name");
ele.set_text("Some content");
ele.add_element(MyPoint{});
let expected = String::from(r#"<?xml version="1.0" encoding="UTF-8"?>
<name>
  <point/>
  Some content
</name>"#);
assert_eq!(ele.to_string_pretty_prolog("\n", "  "), expected);

Trait Implementations

impl Clone for XMLElement[src]

impl Debug for XMLElement[src]

impl Display for XMLElement[src]

impl From<&'_ XMLElement> for XMLElement[src]

impl PartialEq<XMLElement> for XMLElement[src]

impl StructuralPartialEq for XMLElement[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.