Link to home
Start Free TrialLog in
Avatar of Mero
Mero

asked on

outputting an xml output to the system.out

Hi,

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.
Avatar of Mero
Mero

ASKER

I've increased the points.

Or does anybody know any other way to output the org.w3c.dom.Document to the system.out.println?
"Writing Out a DOM as an XML File"

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


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 =
           TransformerFactory.newInstance();

 Transformer serializer = transformerFactory.newTransformer();

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

HTH
Avatar of Mero

ASKER

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]"!
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.

OK?
Avatar of Mero

ASKER

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.
"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()}
     else
        str = str + toXML(children[i]);
     }
   return str;

}

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

ASKER

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.AbstractMethodError
     java.lang.Throwable()
     java.lang.Error()
     java.lang.LinkageError()
     java.lang.IncompatibleClassChangeError()
     java.lang.AbstractMethodError()
     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)
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mero

ASKER

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.