Generating text document from template dynamically using values from java objects
I want to create a Word document from an HTML page. I am planning to get the values in an HTML page and then pass those values to the document template. I have used JSOUP to parse HTML page content and I am getting the values in my java program. Now I want to pass these values into a Word document template. I want to know what are the best techniques I can use to create a document template and pass values to the template to create a Word document.
Thank.
source to share
I suggest you use xslt because your data is already in xml format and Microsoft has well defined xml formats.
You can write document template with word and save it in xml format. Then you can convert word-xml to xsl template with your html-xml as input. After the xslt transformation, you have a valid word-xml with your dynamic values from the html-xml.
XSLT example for excel
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no" />
<xsl:template match="/">
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
...
<xsl:for-each
select="/yourroot/person">
...
<Cell ss:StyleID="uf">
<Data ss:Type="String">
<xsl:value-of
select="@Name" />
</Data>
</Cell>
..
</xsl:for-each>
...
</xsl:template>
</xsl:stylesheet>
source to share
JODReports and Docmosis might also be helpful for you as there is a fill pattern and Doc output. If DOCX is your real goal, you can write the document yourself, since the XML is published, but that's a lot of work.
source to share