Rendering HTML using HTMLEditorKit


2003-08-22 09:46:04 PM
jbuilder13
I am using XSLT to tranform an XML document into HTML. I would then like to
display that HTML in my Java application. I read that I should use the
HTMLEditorKit in conjunction with a JEditorPane. Here is a snippet of my
code:
try {
PipedOutputStream xsltResultOut = new
PipedOutputStream();
PipedInputStream xsltResultIn = new
PipedInputStream(xsltResultOut);
DOMSource xmlSource = new DOMSource(doc); //doc is a
Document object (xml)
StreamSource xsltSource = new StreamSource(new
File("xsl/myFile.xslt"));
StreamResult theResult = new
StreamResult(xsltResultOut);
TransformerFactory transFact =
TransformerFactory.newInstance();
Transformer trans =
transFact.newTransformer(xsltSource);
trans.transform(xmlSource, theResult);
xsltResultOut.close();
BufferedReader br = new BufferedReader(new
InputStreamReader(xsltResultIn));
htmlKit.read(br, editorPane.getDocument(), 0);
xsltResultIn.close();
}
catch (BadLocationException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (TransformerConfigurationException ex) {
ex.printStackTrace(System.err);
}
catch (TransformerException ex) {
ex.printStackTrace(System.err);
}
The text that is displayed in my JEditorPane is:
?xml version="1.0" encoding="UTF-8"?>followed by the text content of all
the tags in the XML document
Note: if I changed my XML Source to:
StreamSource xmlSource = new javax.xml.transform.stream.StreamSource(new
File("scenario.xml")); //reading from a file
my program just hangs never displaying anything in my EditorPane.
However, if I change the theResult to:
StreamResult theResult = new StreamResult(System.out); my console shows the
correct html.
So, I am bit confused what the problem is. If anyone has any suggestions, I
would appreciate it.