public String createTable() { Element root = new Element("table"); root.setAttribute("border", "0"); Document doc = new Document(root); Element thead = new Element("thead"); Element th = new Element("th"); th.addContent("A header"); th.setAttribute("class", "aka_header_border"); thead.addContent(th); Element th2 = new Element("th"); th2.addContent("Another header"); th2.setAttribute("class", "aka_header_border"); thead.addContent(th2); root.addContent(thead); Element tr1 = new Element("tr"); Element td1 = new Element("td"); td1.setAttribute("valign", "top"); td1.setAttribute("class", "cellBorders"); td1.setText("cell contents"); tr1.addContent(td1); root.addContent(tr1); XMLOutputter outp = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setOmitDeclaration(true); outp.setFormat(format); Writer writer = new StringWriter(); try { outp.output(doc, writer); } catch (IOException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } return writer.toString(); }
public static void outputDocument(Document document, Writer out) throws IOException { XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getPrettyFormat().setEncoding("UTF-8")); outputter.output(document, out); }