Docmosis looks very powerful and useful. According to the statement on the features page the following types of data sources are usable:
* Java Objects (POJOs)
* Databases - MySQL, Oracle ..
* delimited files - CSV, PSV etc
* you name it
Unfortunately our data is provided in XML. Is there a way to use XML as a data source? If not, do you provide some kind of provision to extend your product? What are your recommendations about using this very common data structure?
-Rusty
public static void main (String argv []) throws Exception
{
// Load the XML document
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
String xmlfile="C:/myproject/customers.xml";
Document doc = docBuilder.parse (new File(xmlfile));
doc.getDocumentElement ().normalize ();
// convert the XML document into data for Docmosis
MemoryDataProvider dp = buildFromXML(doc);
// optionally add more data
DataProviderBuilder dpb = new DataProviderBuilder(dp);
dpb.add("time", new Date().toString());
// render the tempate (in.doc) with the XML document data
File in = new File("c:/myproject/in.doc");
File out = new File("c:/myproject/out.doc");
try {
SystemManager.initialise();
DocumentProcessor.renderDoc(in, out, dp);
} finally {
SystemManager.release();
}
}
public static MemoryDataProvider buildFromXML(org.w3c.dom.Document doc)
{
return buildFromXML(doc.getChildNodes(), new MemoryDataProvider());
}
private static MemoryDataProvider buildFromXML(NodeList nodes, MemoryDataProvider dp)
{
if (nodes == null) {
return null;
}
int len = nodes.getLength();
for(int i=0; i < len; i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getChildNodes().getLength() == 1 && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
dp.setString(node.getNodeName(), node.getFirstChild().getNodeValue());
} else {
dp.addDataProvider(node.getNodeName(), buildFromXML(node.getChildNodes(), new MemoryDataProvider()));
}
}
}
return dp;
}
Hope that helps.
Hi,
I am also interested in using XML as datasource so I would love to see this feature implemented soon.
Regards
Bert
Hello Bert.
Thanks for letting us know this is important to you. Since few people have asked, we assumed it was fairly low on the priority list.
We'll bump it's priority and try to add it to 2.2.1 or 2.2.2.
Hi,
As per our requirement, we will generate the XML tags based on place holders in templates and xml file tags that are generated may vary from template to template in our case. The xml tags are as shown below and property values are substituted once hit database.
customer_first_name
customer_last_name
customer_address_1
customer_address_2
customer_city
customer_state
customer_zip_code
So, i have a clarification like does Docmosis or other API supports to place the xml data in templates!
Hello srinadh.
You can populate documents in Docmosis using the code above, but built-in XML support has been developed and is undergoing testing as part of the next major release of Docmosis. This is due for release later this year.
Hi,
As i have gone through the sample code that was provided in this thread, understand that, we can replace/substitute any desired string that is in the input/output document during render the document using Docmosis API.
-----
MemoryDataProvider dp = new MemoryDataProvider();
dp.setString("loan_policy_number", "Srinadh".toString());
File templateFile = new File("C:/Workspace/ALTA11L.rtf");
File resultFile = new File("C:/Workspace/DocMosisTemp.pdf");
DocumentProcessor.renderDoc(templateFile, resultFile, dp);
-----
I could not able to replace the "loan_policy_number" with the string "Srinadh" in my 'resultFile' and not
generated any exception.
Please suggest me the right way to replace the "loan_policy_number" with the string "Srinadh" in output document.
Hello Srinadh.
It sounds like your template isn't a template as far as Docmosis is concerned:
1) it needs to be a DOC or ODT format file
2) you need to use merge fields - plain text is not substituted - that would be a bad idea.
In your case, you need to:
1) turn "ALTA11L.rtf" into a doc (load with Word and save as doc)
2) where you have "loan_policy_number" in your template, you need a Merge Field "loan_policy_template". The Template Developer Guide on in the support area (http://www.docmosis.com/support) tells you how to insert merge fields.
I suggest you read the Template Developer Guide which should give you a lot of help.
please provide working example reading xml.
and how it work with oracle ???
need to load xml from clob or blob
please may you attach example with reading xml, I would be great full if not difficult.
Hello troysev.
Please look higher up in this post to see the example of using XML as data for Docmosis.
The process of reading from Oracle will be:
1) Query the database
2) read the clob into an XML string
3) use the code example above to convert the XML into a DataProvider for Docmosis
4) render the document.
The specifics of fetching Oracle clob data are outside the scope of this forum, but there are lots of examples on the net of reading them from Oracle.
