Function to serialize to stream.
authorScott Cantor <cantor.2@osu.edu>
Thu, 16 Nov 2006 19:45:03 +0000 (19:45 +0000)
committerScott Cantor <cantor.2@osu.edu>
Thu, 16 Nov 2006 19:45:03 +0000 (19:45 +0000)
xmltooling/util/XMLHelper.cpp
xmltooling/util/XMLHelper.h

index c876e97..d6506a0 100644 (file)
@@ -227,3 +227,36 @@ void XMLHelper::serialize(const DOMElement* e, std::string& buf)
     buf.erase();
     buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
 }
+
+namespace {
+    class StreamFormatTarget : public XMLFormatTarget
+    {
+    public:
+        StreamFormatTarget(std::ostream& out) : m_out(out) {}
+        ~StreamFormatTarget() {}
+
+        void writeChars(const XMLByte *const toWrite, const unsigned int count, XMLFormatter *const formatter) {
+            m_out.write(reinterpret_cast<const char*>(toWrite),count);
+        }
+
+        void flush() {
+            m_out.flush();
+        }
+
+    private:
+        std::ostream& m_out;
+    };
+};
+
+void XMLHelper::serialize(const DOMElement* e, std::ostream& out)
+{
+    static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
+    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
+    DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
+    DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
+    XercesJanitor<DOMWriter> janitor(serializer);
+    serializer->setEncoding(UTF8);
+    StreamFormatTarget target(out);
+    if (!serializer->writeNode(&target,*e))
+        throw XMLParserException("unable to serialize XML");
+}
index 868d00f..c09d3d9 100644 (file)
@@ -26,6 +26,8 @@
 #include <xmltooling/QName.h>
 #include <xercesc/dom/DOM.hpp>
 
+#include <iostream>
+
 using namespace xercesc;
 
 namespace xmltooling {
@@ -219,6 +221,15 @@ namespace xmltooling {
          * @param buf   buffer to serialize element into
          */
         static void serialize(const DOMElement* e, std::string& buf);
+
+        /**
+         * Serializes the DOM Element provided to a stream using UTF-8 encoding and
+         * the default XML serializer available. No manipulation or formatting is applied.
+         * 
+         * @param e     element to serialize
+         * @param out   stream to serialize element into
+         */
+        static void serialize(const DOMElement* e, std::ostream& out);
     };
 
 };