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.
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.
"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.newInst ance();
Transformer serializer = transformerFactory.newTran sformer();
serializer.transform( new DOMSource( document ),
new StreamResult( new FileOutputStream( "intro1.xml" ) ) );
HTH
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.newInst
Transformer serializer = transformerFactory.newTran
serializer.transform( new DOMSource( document ),
new StreamResult( new FileOutputStream( "intro1.xml" ) ) );
HTH
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?
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?
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.
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"!
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"!
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.AbstractMethodEr ror
java.lang.Throwable()
java.lang.Error()
java.lang.LinkageError()
java.lang.IncompatibleClas sChangeErr or()
java.lang.AbstractMethodEr ror()
java.lang.String org.apache.xpath.DOM2Helpe r.getNames paceOfNode (org.w3c.d om.Node)
java.lang.String org.apache.xpath.DOM2Helpe r.getNames paceOfNode (org.w3c.d om.Node)
void org.apache.xml.utils.TreeW alker.star tNode(org. w3c.dom.No de)
void org.apache.xml.utils.TreeW alker.trav erse(org.w 3c.dom.Nod e)
void org.apache.xalan.transform er.Transfo rmerIdenti tyImpl.tra nsform(jav ax.xml.tra nsform.Sou rce, javax.xml.transform.Result )
What can be wrong?
java.lang.AbstractMethodEr
java.lang.Throwable()
java.lang.Error()
java.lang.LinkageError()
java.lang.IncompatibleClas
java.lang.AbstractMethodEr
java.lang.String org.apache.xpath.DOM2Helpe
java.lang.String org.apache.xpath.DOM2Helpe
void org.apache.xml.utils.TreeW
void org.apache.xml.utils.TreeW
void org.apache.xalan.transform
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Or does anybody know any other way to output the org.w3c.dom.Document to the system.out.println?