From 2c3b327739b79d5ba7fe931e205bec1ad320b360 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 8 Jan 2015 18:11:21 +0100 Subject: further extended the advanced document example, slightly improved XML serialization and fixed a bug in DemoOutput leading to errors if a section/paragraph had no heading. --- src/core/XML.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'src/core/XML.cpp') diff --git a/src/core/XML.cpp b/src/core/XML.cpp index 038cb86..7f03b35 100644 --- a/src/core/XML.cpp +++ b/src/core/XML.cpp @@ -4,12 +4,16 @@ namespace ousia { namespace xml { -void Node::serialize(std::ostream& out){ +void Node::serialize(std::ostream &out, const std::string &doctype) +{ out << "\n"; + if (doctype != "") { + out << doctype << "\n"; + } doSerialize(out, 0); } -void Element::doSerialize(std::ostream& out, unsigned int tabdepth) +void Element::doSerialize(std::ostream &out, unsigned int tabdepth) { for (unsigned int t = 0; t < tabdepth; t++) { out << '\t'; @@ -18,17 +22,22 @@ void Element::doSerialize(std::ostream& out, unsigned int tabdepth) for (auto &a : attributes) { out << ' ' << a.first << "=\"" << a.second << '\"'; } - out << ">\n"; - for (auto &n : children) { - n->doSerialize(out, tabdepth + 1); - } - for (unsigned int t = 0; t < tabdepth; t++) { - out << '\t'; + // if we have no children, we close the tag immediately. + if (children.size() == 0) { + out << "/>\n"; + } else { + out << ">\n"; + for (auto &n : children) { + n->doSerialize(out, tabdepth + 1); + } + for (unsigned int t = 0; t < tabdepth; t++) { + out << '\t'; + } + out << "\n"; } - out << "\n"; } -void Text::doSerialize(std::ostream& out, unsigned int tabdepth) +void Text::doSerialize(std::ostream &out, unsigned int tabdepth) { for (unsigned int t = 0; t < tabdepth; t++) { out << '\t'; -- cgit v1.2.3