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 '<' 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)?
> 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
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