pub struct ElementBuilder { /* private fields */ }
Expand description
An easy way to build a new element by chaining methods to add properties.
Call Element::build()
to start building.
To finish building, either call .finish()
or .push_to(parent)
which returns Element
.
§Examples
use edit_xml::{Document, Element, Node};
let mut doc = Document::new();
let root = Element::build("root")
.attribute("id", "main")
.attribute("class", "main")
.finish(&mut doc);
doc.push_root_node(root.as_node());
let name = Element::build("name")
.add_text("No Name")
.push_to(&mut doc, root);
/* Equivalent xml:
<root id="main" class="main">
<name>No Name</name>
</root>
*/
Implementations§
Source§impl ElementBuilder
impl ElementBuilder
Sourcepub fn new(full_name: impl Into<String>) -> ElementBuilder
pub fn new(full_name: impl Into<String>) -> ElementBuilder
Creates a new ElementBuilder with the full name of the element.
Sourcepub fn new_with_capacities(
full_name: impl Into<String>,
attribute_capacity: usize,
namespace_capacity: usize,
content_capacity: usize,
) -> ElementBuilder
pub fn new_with_capacities( full_name: impl Into<String>, attribute_capacity: usize, namespace_capacity: usize, content_capacity: usize, ) -> ElementBuilder
Creates a new ElementBuilder with the full name of the element and the capacities of the attributes, namespace declarations, and content.
Sourcepub fn prefix(self, prefix: &str) -> Self
pub fn prefix(self, prefix: &str) -> Self
Removes previous prefix if it exists, and attach new prefix.
Sourcepub fn namespace_decl<S, T>(self, prefix: S, namespace: T) -> Self
pub fn namespace_decl<S, T>(self, prefix: S, namespace: T) -> Self
Add a namespace declaration to the element.
Sourcepub fn add_text<S: Into<String>>(self, text: S) -> Self
pub fn add_text<S: Into<String>>(self, text: S) -> Self
Add text content to the element.
use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
.add_text("Hello")
.finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_text());
Sourcepub fn add_comment<S: Into<String>>(self, text: S) -> Self
pub fn add_comment<S: Into<String>>(self, text: S) -> Self
Add a comment node to the element.
use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
.add_comment("This is a comment")
.finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_comment());
Sourcepub fn add_cdata<S: Into<String>>(self, text: S) -> Self
pub fn add_cdata<S: Into<String>>(self, text: S) -> Self
Add a CDATA node to the element.
use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
.add_cdata("This is a CDATA")
.finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_cdata());
Sourcepub fn add_pi<S: Into<String>>(self, text: S) -> Self
pub fn add_pi<S: Into<String>>(self, text: S) -> Self
Add a Processing Instruction node to the element.
use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
.add_pi("target")
.finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert!(content[0].is_pi());
Sourcepub fn add_element(self, elem: ElementBuilder) -> Self
pub fn add_element(self, elem: ElementBuilder) -> Self
Add an element to the element.
Sourcepub fn create_element<F>(self, name: impl Into<String>, f: F) -> Self
pub fn create_element<F>(self, name: impl Into<String>, f: F) -> Self
Adds an element to the element.
use edit_xml::{Document, Element};
let mut doc = Document::new();
let root = Element::build("root")
.create_element("child", |builder| {
builder
.add_text("Hello")
}).finish(&mut doc);
let content = root.children(&doc);
assert_eq!(content.len(), 1);
assert_eq!(content[0].text_content(&doc), "Hello");
Sourcepub fn finish(self, doc: &mut Document) -> Element
pub fn finish(self, doc: &mut Document) -> Element
Finish building the element and return it. The result must be pushed to a parent element or the root node.
use edit_xml::{Document, Element, ElementBuilder};
let mut doc = Document::new();
let root = ElementBuilder::new("root")
.add_text("Hello")
.finish(&mut doc);
doc.push_root_node(root.as_node());
Sourcepub fn push_to_root_node(self, doc: &mut Document) -> Element
pub fn push_to_root_node(self, doc: &mut Document) -> Element
Push this element to the root node.
use edit_xml::{Document, Element, ElementBuilder};
let mut doc = Document::new();
let root = ElementBuilder::new("root")
.add_text("Hello")
.push_to_root_node(&mut doc);
assert_eq!(doc.root_element().unwrap(), root);
Trait Implementations§
Source§impl Clone for ElementBuilder
impl Clone for ElementBuilder
Source§fn clone(&self) -> ElementBuilder
fn clone(&self) -> ElementBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more