diff --git a/src/attribute.rs b/src/attribute.rs new file mode 100644 index 00000000..ac41f128 --- /dev/null +++ b/src/attribute.rs @@ -0,0 +1,24 @@ +use xml::escape::escape_str_attribute; + +use std::fmt; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Attribute { + pub name: String, + pub value: String, +} + +impl fmt::Display for Attribute { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) + } +} + +impl Attribute { + pub fn new, V: Into>(name: N, value: V) -> Attribute { + Attribute { + name: name.into(), + value: value.into(), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index f5247722..51a83732 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate xml; pub mod error; +pub mod attribute; use std::io::prelude::*; @@ -15,31 +16,11 @@ use std::fmt; use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter}; use xml::name::Name; -use xml::escape::escape_str_attribute; use xml::namespace::NS_NO_PREFIX; use error::Error; -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Attribute { - pub name: String, - pub value: String, -} - -impl fmt::Display for Attribute { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value)) - } -} - -impl Attribute { - pub fn new, V: Into>(name: N, value: V) -> Attribute { - Attribute { - name: name.into(), - value: value.into(), - } - } -} +use attribute::Attribute; #[derive(Clone, PartialEq, Eq)] pub struct Element { @@ -131,7 +112,13 @@ impl Element { let attributes = attributes.into_iter() .map(|o| Attribute::new(o.name.local_name, o.value)) .collect(); - let mut root = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let mut root = Element::new(name.local_name, ns, attributes); root.from_reader_inner(reader); return Ok(root); }, @@ -151,7 +138,13 @@ impl Element { let attributes = attributes.into_iter() .map(|o| Attribute::new(o.name.local_name, o.value)) .collect(); - let elem = Element::new(name.local_name, namespace.get(NS_NO_PREFIX).map(|s| s.to_owned()), attributes); + let ns = if let Some(ref prefix) = name.prefix { + namespace.get(prefix) + } + else { + namespace.get(NS_NO_PREFIX) + }.map(|s| s.to_owned()); + let elem = Element::new(name.local_name, ns, attributes); let elem_ref = self.append_child(elem); elem_ref.from_reader_inner(reader); },