Skip to content

Instantly share code, notes, and snippets.

@GilesBathgate
Created April 28, 2016 20:28
Show Gist options
  • Save GilesBathgate/d498f7dd6a21ac070d84c2b132d7726a to your computer and use it in GitHub Desktop.
Save GilesBathgate/d498f7dd6a21ac070d84c2b132d7726a to your computer and use it in GitHub Desktop.
import std.xml;
import std.string;
import std.container.stack;
class XmlWriter
{
this()
{
m_stack = new Stack!Element();
}
void writeStartDocument()
{
}
void writeDefaultNamespace(in string value)
{
if (m_element)
{
writeAttribute("xmlns", value);
}
else
{
m_namespace = value;
}
}
void writeStartElement(in string name)
{
if (!m_element)
{
m_element = new Document(new Tag(name));
if (m_namespace)
writeAttribute("xmlns", m_namespace);
}
else
{
m_stack.push(m_element);
m_element = new Element(name);
}
}
void writeAttribute(in string name, in string value)
{
m_element.tag.attr[name] = value;
}
void writeTextElement(in string name, in string value)
{
m_stack.push(m_element);
m_element = new Element(name, value);
writeEndElement();
}
void writeEndElement()
{
if (!m_stack.empty)
{
auto parent = m_stack.pop();
parent ~= m_element;
m_element = parent;
}
}
void writeEndDocument()
{
}
override string toString()
{
if (!m_autoformatting)
return m_element.toString();
return join(m_element.pretty(m_indent), "\n");
}
@property autoFormatting(in bool value)
{
m_autoformatting = value;
}
@property autoFormattingIndent(in int value)
{
m_indent = value;
}
private:
bool m_autoformatting;
int m_indent = 4;
string m_namespace;
Element m_element;
Stack!Element m_stack;
unittest
{
{
auto xml = new XmlWriter();
xml.writeStartDocument();
xml.writeDefaultNamespace("http://example.com");
xml.writeStartElement("Root");
xml.writeEndElement();
xml.writeEndDocument();
assert(xml.toString() == "<?xml version=\"1.0\"?><Root xmlns=\"http://example.com\" />");
}
{
auto xml = new XmlWriter();
xml.writeStartDocument();
xml.writeStartElement("Root");
xml.writeDefaultNamespace("http://example.com");
xml.writeEndElement();
xml.writeEndDocument();
assert(xml.toString() == "<?xml version=\"1.0\"?><Root xmlns=\"http://example.com\" />");
}
{
auto xml = new XmlWriter();
xml.writeStartDocument();
xml.writeStartElement("Root");
xml.writeAttribute("xmlns", "http://example.com");
xml.writeEndElement();
xml.writeEndDocument();
assert(xml.toString() == "<?xml version=\"1.0\"?><Root xmlns=\"http://example.com\" />");
}
{
auto xml = new XmlWriter();
xml.writeStartDocument();
xml.writeStartElement("Root");
xml.writeStartElement("Child1");
xml.writeTextElement("SubChild1", "Foo");
xml.writeEndElement();
xml.writeStartElement("Child2");
xml.writeAttribute("simple", "easy");
xml.writeEndElement();
xml.writeStartElement("Child3");
xml.writeAttribute("simple", "easy");
xml.writeEndElement();
xml.writeEndElement();
xml.writeEndDocument();
assert(xml.toString() == "<?xml version=\"1.0\"?><Root><Child1><SubChild1>Foo</SubChild1></Child1><Child2 simple=\"easy\" /><Child3 simple=\"easy\" /></Root>");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment