javaclojurecyberneko

serialize a NekoHTML ElementNSImpl object back to HTML/XML


Does anyone know if there is a straightforward way to serialize a parsed cyberneko ElementNSImpl object?

Here is my example in Clojure of serializing the whole DOM (an HTMLDocumentImpl object). This works, but I have not yet figured out how to do this for an element from the dom (ElementNSImpl).

(defn dom->xml
  [dom]
  (let [sw (java.io.StringWriter.)] 
    (.serialize 
     (org.apache.xml.serialize.XMLSerializer. 
      sw (org.apache.xml.serialize.OutputFormat. dom)) 
     dom)
    (.toString sw)))

Thanks, Rob


Solution

  • This works for outputting XML, but I still don't know how to output HTML:

    (defn dom->xml
      "serialize a dom element back to XML text"
      [elem]
      (let [sw (java.io.StringWriter.)]
        (.serialize
         (org.apache.xml.serialize.XMLSerializer. 
          sw (org.apache.xml.serialize.OutputFormat.))
         elem)
        (str sw)))