I was wondering why DOMSerializer.serialize takes only Document and NOT
a node?
Any thoughts? What if I have a node to serialize?

Thanks in advance.

-Sanjay

-----Original Message-----
From: Simon Kitching [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, April 23, 2003 6:12 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: How to print XML Node


On Thu, 2003-04-24 at 12:39, Sanjay Gupta (sanjaygu) wrote:
> Hi,
> I have a Xml dom node and I want to print it to a file. I want all the

> node types to printed like element, doctype and comment nodes. Does 
> anyone has a idea how to print it. Is there a convenient API?
>  
> -Sanjay

You can use the  following classes:
org.apache.xml.serialize.XMLSerializer;
org.apache.xml.serialize.DOMSerializer;

Here is an extract of some code I use to perform this task:


  OutputFormat outputFormat = new OutputFormat();
  outputFormat.setMethod(Method.XML);
  outputFormat.setIndenting(true);
  outputFormat.setIndent(4);
        
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  XMLSerializer xmlSerializer = new XMLSerializer(
     baos, outputFormat);
  DOMSerializer domSerializer = xmlSerializer.asDOMSerializer();
  domSerializer.serialize(doc);
  byte[] data = baos.toByteArray();

You can then write the byte array to a file. 

I specifically wanted a byte array for other reasons; because you only
want to write to a file, you might be able to pass a FileOutputStream to
the XMLSerializer or similar.

Regards,

Simon



Reply via email to