Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why is DOM -> String so damn tricky in java

0 views
Skip to first unread message

anonymous coward

unread,
Jul 11, 2002, 11:15:11 AM7/11/02
to
XML would be useless as a cross platform data format if it wasn't for the fact that it boils down to a string, so why is it so damn complicated to convert a DOM Document to a String in Java? I would only consider it THE most fundamental thing to want to do with a storage class - get at what it is storing and all that.

Anyway, some help please.

I am writing an applet that needs to turn a Document into a String. As this is an applet I only want to use standard classes that come with jre 1.4 to keep things as small as possible. I have tried the following:

String doc2string( Document doc )
{
try
{
StringWriter ret = new StringWriter();

TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();
Source input = new DOMSource( doc );
Result output = new StreamResult( ret );

idTransform.transform( input, output );

return ret.toString();
}
catch ( Exception e )
{
System.err.println( e );
return "";
}
}

and it works just fine, until i use it in an applet using appletviewer or mozilla when I get this lovely error:

org.apache.xml.utils.WrappedRuntimeException: Output method is text could not load output_xml.properties (check CLASSPATH)
at org.apache.xalan.templates.OutputProperties.getDefaultMethodProperties(OutputProperties.java:364)

does anyone have any suggestions as to a reliable technique for getting the XML out of an XML container? I have searched high and low and found nothing more than : just download this more useful implementation as it makes it easy, but I don't think a 6MB download on a web page is really very sensible...

alternatively if there is an xml escape function (String->String with '&lt;' instead of '<' etc.) lurking around in the standard libraries I could dispense with the DOM alltogether which would be nice.

many thanks to anyone who can help, I have a renewed sense of exhasperation with shoddy APIs which coincides with a new respect for the programmers who have to put up with them - how do you do it?

was a Document.toString() method just too radical for sun/xalan (or whoever else implemented the DOM I am using, I have no idea)?

NBBhav

unread,
Jul 11, 2002, 6:58:53 PM7/11/02
to
anonymous coward wrote:

> was a Document.toString() method just too radical for sun/xalan (or
> whoever else implemented the DOM I am using, I have no idea)?

Try, where doc is your Document:

doc.getDocumentElement().toString();

It gives the XML, but no pretty printing (indenting, etc.) with it.


Bhav

Nicholas Clare

unread,
Jul 12, 2002, 4:27:34 AM7/12/02
to
bh...@NOSPAMbhav.co.uk (NBBhav) wrote in <K5oX8.3139$Ro6.205693@newsfep1-
win.server.ntli.net>:

If you use the SUN jaxp.jar for XML manipulation you can maintain the
spacing so it's human readable by using the doc.write() method with a
stream such as System out:

doc.write(System.out);

or a file:

FileOutputStream fo = new FileOutputStream(f);
doc.write(fo);

Cheers,
Nik

0 new messages