#ifndef __XMLWRITER_H__ #define __XMLWRITER_H__ #include #include #include /** This class is a class which helps printing formated xml output. * Example: * This sequence: * xml.openTag("world"); * xml.writeAttribute("name", "foo"); * xml.writeTag("bar"); * xml.writeTag("baz"); * xml.writeAttribute("name", "boo"); * xml.writeAttribute("style", "old"); * xml.write("text"); * xml.closeTag("world"); * results in this output: * * * text * */ class XmlWriter { public: XmlWriter(std::ostream& out); ~XmlWriter(); /** Start a xml tag which contains subtags */ void openTag(const char* name); /** Closes an xml tag with subtags */ void closeTag(const char* name); void writeTag(const char* name); template void comment(const T& outp) { // This routine writes just about anything as an XML comment. newLine(); out << ""; } template void write(const T& text) { if (closetag[0]=='>') { out << ">"; closetag = ""; } else if (closetag[0]=='/') { out << ">"; // eventually we should place a \n here closetag = ""; } out << text; } template void writeAttribute(const char* name, T value) { out << " " << name << "=\"" << value << "\""; } private: void newLine(); void closeTag(); std::ostream& out; int indent; std::string closetag; std::string lasttag; std::vector sections; }; #endif