You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by "Sanjay Gupta (sanjaygu)" <sa...@cisco.com> on 2003/04/24 02:39:10 UTC

How to print XML Node

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

RE: How to print XML Node

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Fri, 2003-04-25 at 09:48, Sanjay Gupta (sanjaygu) wrote:
> I was wondering why DOMSerializer.serialize takes only Document and NOT
> a node?
> Any thoughts? What if I have a node to serialize?

There is a serialize(Element e) method.

Provided your node is an element, just cast it to the correct type.

Clearly, serializing an attribute node on its own (or text node, or ..)
is not supported, but there really shouldn't be much demand for this!




RE: How to print XML Node

Posted by "Sanjay Gupta (sanjaygu)" <sa...@cisco.com>.
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:simon@ecnetwork.co.nz] 
Sent: Wednesday, April 23, 2003 6:12 PM
To: sanjaygu@cisco.com
Cc: xalan-j-users@xml.apache.org
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




Re: How to print XML Node

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
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