• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

How to print a Document in JAXP 1.1

HI,
As we know, it is easy to parse a .xml file and construct a Document object using DocumentBuilder object. However, I need to do the reverse construction. That's to say, I have a Document object, and need print the Document to a file.   Does anybody know how to do it?

Thanks, bob.
0
bobli
Asked:
bobli
  • 2
1 Solution
 
mattykCommented:
Take a look at the page Writing Out a DOM as an XML File
 at:

http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/2_write.html


In version 1.0 you could use the XMLDocument interface that Sun provided.  
In JAXP 1.1 there is a new method to do this:

 import javax.xml.transform.*;
 import javax.xml.transform.dom.*;
 import javax.xml.transform.stream.*;

 //normal xml code to create DOM called document goes here

 //now write out to FileStream
 TransformerFactory transformerFactory =
            TransformerFactory.newInstance();

  Transformer serializer = transformerFactory.newTransformer();

 serializer.transform( new DOMSource( document ),
            new StreamResult( new FileOutputStream( "intro1.xml" ) ) );




-matty

 
0
 
mattykCommented:
Bobli,

  Any luck with this?  

-matty
0
 
bobliAuthor Commented:
Yes, It works fine. Thanks matty.

bob
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now