Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

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.
0
Mero
Asked:
Mero
  • 5
  • 4
1 Solution
 
MeroAuthor Commented:
I've increased the points.

Or does anybody know any other way to output the org.w3c.dom.Document to the system.out.println?
0
 
BigRatCommented:
"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
0
 
MeroAuthor Commented:
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]"!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BigRatCommented:
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?
0
 
MeroAuthor Commented:
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.
0
 
BigRatCommented:
"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"!
0
 
MeroAuthor Commented:
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)
0
 
BigRatCommented:
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...."?
0
 
MeroAuthor Commented:
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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