Convert DOM Document to String

Discussions

Web tier: servlets, JSP, Web frameworks: Convert DOM Document to String

  1. Convert DOM Document to String (1 messages)

    Hi All, I am trying to generate PDFs using tidy and iText. I want to show the jsp page in PDF format. I have 100Jsp and I am putting PDF icon in every page. When I click on the icon I want to show PDF page similar to JSp page. Here I also posting the code just look at that, //For testing purpose we are sending hard coded one String htmlFile = "TEST

    this is test page

    "; //String htmlFile = request.getParameter("html_input"); //To read the html string ByteArrayInputStream is = new ByteArrayInputStream(htmlFile.getBytes()); ByteArrayOutputStream os = new ByteArrayOutputStream(); // step 1: creation of a document-object Document document = new Document(PageSize.A4, 40, 40, 40, 40); try { //To clean up the web page and tidying setting the attributes to tidy object and parseing it. Tidy tidy = new Tidy(); tidy.setTidyMark(false); tidy.setDocType("auto"); tidy.setWrapScriptlets(true); tidy.setOnlyErrors(true); tidy.setXHTML(true); tidy.setEncloseText(true); tidy.setXmlTags(true); tidy.setBreakBeforeBR(true); String newString = tidy.parseDOM(is, os).toString(); PdfWriter.getInstance(document, response.getOutputStream()); System.out.println("PDF Writer getInstance................."); document.open(); System.out.println("Doc open................."); /*Chunk chunk = new Chunk (newString); document.add (chunk); System.out.println("Doc Chunkline.................");*/ //Paragraph para = new Paragraph(); document.add (new Paragraph("test ")); System.out.println("Doc paragraph................."); document.add (new Paragraph(" " + htmlFile)); System.out.println("Doc paragraph htmlFile................."); document.add(Chunk.NEWLINE); System.out.println("Doc Chunkline................."); document.add(new Paragraph("The method used to generate this PDF was: JITU" )); System.out.println("Doc Message................."); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } finally { document.close(); //is.close(); os.close(); } in the line String newString = tidy.parseDOM(is, os).toString(); I am getting DOM Document and I am trying to convert into String, but the value is empty. Can any one HELP me out in this regard......how toconvert DOM-Ducument to string and then PDFs file. If you have any other technics or other ideas.....please let me know.... Thanks in advance.....
  2. import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; public class DOMUtil { ... public static StringBuilder convert( Document doc) { StringBuilder stringBuilder = null; try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); OutputFormat outputformat = new OutputFormat(); outputformat.setIndent(4); outputformat.setIndenting(true); outputformat.setPreserveSpace(false); XMLSerializer serializer = new XMLSerializer(); serializer.setOutputFormat(outputformat); serializer.setOutputByteStream(stream); serializer.asDOMSerializer(); serializer.serialize(doc.getDocumentElement()); stringBuilder = new StringBuilder(stream.toString()); } catch (Exception except) { except.getMessage(); } return stringBuilder; } }