outputting an xml output to the system.out

Posted on 2003-02-24
Medium Priority
Last Modified: 2008-03-06

I have an xml stream I'm trying to output to the system.out.println.   My xml doc is org.w3c.dom.Document.

I was told a while back that I can somehow output my xml by using the TransformerFactory and then use a DOMSource in order to output it.

Does anybody know how to do this exactly?  Thanks.
Question by:Mero
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4

Author Comment

ID: 8016604
I've increased the points.

Or does anybody know any other way to output the org.w3c.dom.Document to the system.out.println?
LVL 27

Expert Comment

ID: 8017040
"Writing Out a DOM as an XML File"


In JAXP 1.1 :-

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

//now write out to FileStream
TransformerFactory transformerFactory =

 Transformer serializer = transformerFactory.newTransformer();

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


Author Comment

ID: 8020831
is there another way to output to the xmldoc to a string?  I'm trying to use the transformer stuff but it turns out to be opening a can of worms because I get constant errors that I am missing a bunch of classes.  Is there a simple way?!  If I try a toString() method, all it returns is a "[#document: null]"!
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

LVL 27

Expert Comment

ID: 8025016
There is no toString() method. Microsoft has an xml() method which returns an XML string - but the other people think that this is too simple.

I've put it into a file, but to put it into a string you'll need to replace my FileOutputStream with a StringWriter :-

StringWriter sw = new StringWriter();

<code as above but sw replaces new FileOutputStream( "intro1.xml" )>

then sw.toString() gets the string out of the StringBuffer object in the StringWriter.


Author Comment

ID: 8025782
Yes, there is a toString() method and I have used it(at least in my dom, but it is not listed in any Javadoc).

But like I said above, I'm trying to avoid right now using Transformers because when I use it, I get unsatisfield link errors because its looking for a bunch of classes I don't have.  Isn't there an easy way to get the xml?

I am using the IBM Parser for Java.
LVL 27

Expert Comment

ID: 8027123
"Yes, there is a toString() method and I have used it(at least in my dom, but it is not listed in any Javadoc)."

and does this return the XML string representation for the DOM? (as in Microsoft)?

If not you're goind to have to do it by hand :-

private toXML(Node node) {
   String str = '';
   children = node.getChildren();
   for (......) {
     if children[i]=TEXT_NODE then
        str = str + children[i].text()}
        str = str + toXML(children[i]);
   return str;


if you get what I mean without me writing syntatically correct Java"!

Author Comment

ID: 8044453
I was able to get the classes that the tranformfactory needs but when I try to use your suggestion to convert it to a string, I get the following error below:

What can be wrong?
     java.lang.String org.apache.xpath.DOM2Helper.getNamespaceOfNode(org.w3c.dom.Node)
     java.lang.String org.apache.xpath.DOM2Helper.getNamespaceOfNode(org.w3c.dom.Node)
     void org.apache.xml.utils.TreeWalker.startNode(org.w3c.dom.Node)
     void org.apache.xml.utils.TreeWalker.traverse(org.w3c.dom.Node)
     void org.apache.xalan.transformer.TransformerIdentityImpl.transform(javax.xml.transform.Source, javax.xml.transform.Result)
LVL 27

Accepted Solution

BigRat earned 600 total points
ID: 8056764
My code is quite trivial. The error you have seems to suggest that the installation of Xalan is corrupt, invalid or messed up.

I would start again by reinstalling it all. Incidentally on what platform are you developing? And what do you mean by "I was able to get the classes...."?

Author Comment

ID: 8112821
I gave up on using the transformer and displaying the xml for now so I'll be generous enough to give you my 200 points for your help.  I am just looping through the nodes right now.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question