Originally posted by Lasse Koskela:
Could you post the piece of code where you create the parameters for transform() and a couple of lines from the beginning of your XML document?
Servlet Code : public void doPost(HttpServletRequest req, HttpServletResponse res) {
try{
res.setContentType( "text/html; charset=UTF-8");
PrintWriter out = res.getWriter();
//String strObjectXML = XML Packet from the database.
strObjectXML = new String(strObjectXML.getBytes("UTF-8"));
//create object of Transformer class
DemoTransform objDT = new DemoTransform();
String html = objDT.generateHTML(strObjectXML, xslPath);
out.println(html);
out.close();
}
XML Packet <?xml version="1.0" encoding="UTF-8" ?>
<root language="thai">
<message_text id="msg001">เครื่องบิน</message_text>
<message_text id="msg002">แล้ว</message_text>
<message_text id="msg003">แล้ว</message_text>
<message_text id="msg004">เก่า</message_text>
<message_text id="msg005">เก่า</message_text>
<message_text id="msg006">เก่า</message_text>
<message_text id="msg007">ด้วย</message_text>
<message_text id="msg008">เก่า</message_text>
<message_text id="msg009">เครื่องบิน</message_text>
<message_text id="msg010">ไม่ดี</message_text>
<message_text id="msg011">� องบินครื่งบินอพาร์ตเมนต์</message_text>
<message_text id="msg012">เก่า</message_text>
<message_text id="msg013">ไม่ดี</message_text>
</root>
DemoTransform.java public class DemoTransform {
public String generateHTML(String xmlData, String xslPath){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String html = null;
try{
TransformerFactory myTF = TransformerFactory.newInstance();
Templates tmplXsl = myTF.newTemplates(new StreamSource(new File(xslPath)));
Transformer transformer = tmplXsl.newTransformer();
//read xml Data in a byte array
byte bXml[] = new byte[xmlData.length()];
bXml = xmlData.getBytes();
//create a ByteArrayInputStream on the byte array of xml data
ByteArrayInputStream bais = new ByteArrayInputStream(bXml);
baos = new ByteArrayOutputStream();
StreamResult sr=new StreamResult();
sr.setOutputStream(baos);
transformer.transform(new StreamSource(bais), sr);
html=baos.toString("UTF-8");
baos.close();
bais.close();
}
catch (Exception e){
System.out.println("EXCEPTION = " + e);
e.printStackTrace();
}
return html;
}
[ April 22, 2004: Message edited by: Ashish Agrawal ]