You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by ja...@javaboss.com on 2000/06/29 17:51:15 UTC

DOM Serialization

I'm trying to serialise a node from a dom document.  I was using:


    private String nodeToString(Node node) throws SAXException {
        StringWriter stringWriter = new StringWriter();
        FormatterToXML fl = new FormatterToXML(stringWriter);
        TreeWalker tw = new TreeWalker(fl);
        tw.traverse(node);
        fl.flush();
        fl.flushWriter();
        return stringWriter.toString();
    }

This works, but I get & turned into &, so when I read in the XML from a file
again - it's invalid.

So I've tried using serializers as the docs say, but they all require a document
or a document fragment.

Can anyone show me a snippet of code that will take a node and turn it into a
string leaving the & and other encoded characters intact!

Many Thanks,

Neil.



---------------------------------------------
This message was sent using Endymion MailMan.
http://www.endymion.com/products/mailman/



Re: DOM Serialization

Posted by Ryan Schmidt <ry...@cpsc.ucalgary.ca>.
XMLSerializer has a serialize(Element) method. It also has a protected
serializeNode(Node) method, so you may want to try subclassing it and
adding a serialize(Node) method that calls serializeNode(). 

This will preserve &amp;&quot;&lt;&gt; etc, but check the mail archives
for some
problems with other encoded characters (&#xx;)

-RMS

javaboss@javaboss.com wrote:
> 
> I'm trying to serialise a node from a dom document.  I was using:
> 
>     private String nodeToString(Node node) throws SAXException {
>         StringWriter stringWriter = new StringWriter();
>         FormatterToXML fl = new FormatterToXML(stringWriter);
>         TreeWalker tw = new TreeWalker(fl);
>         tw.traverse(node);
>         fl.flush();
>         fl.flushWriter();
>         return stringWriter.toString();
>     }
> 
> This works, but I get &amp; turned into &, so when I read in the XML from a file
> again - it's invalid.
> 
> So I've tried using serializers as the docs say, but they all require a document
> or a document fragment.
> 
> Can anyone show me a snippet of code that will take a node and turn it into a
> string leaving the &amp; and other encoded characters intact!
> 
> Many Thanks,
> 
> Neil.
> 
> ---------------------------------------------
> This message was sent using Endymion MailMan.
> http://www.endymion.com/products/mailman/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org