10 replies [Last post]
rustyspoone
Offline
Joined: 2010-06-23

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

admin
Offline
Joined: 2010-09-06
Options
Hi Rustyspoone. There's no built-in support directly for XML data extraction in the 2.0 release. Your current options are: 1) Traverse your XML document and build the structure yourself (code example below) 2) Implement your own implementation of the DataProvider interface that extracts the data from your XML documents We have this as an outstanding enhancement. If enough people are interested in such a feature we can raise the priority to get it released earlier. Here's a complete example that loads an XML file, builds the data structure, and renders a document from it
   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.

bc
Offline
Joined: 2010-05-11
Also interested

Hi,

I am also interested in using XML as datasource so I would love to see this feature implemented soon.

Regards

Bert

modman
Offline
Joined: 2010-11-25
Projected Implementation

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.

srinadh
Offline
Joined: 2010-10-22
Hi, As per our requirement,

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!

admin
Offline
Joined: 2010-09-06
XML Support is coming in Release 2.3

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.

srinadh
Offline
Joined: 2010-10-22
Hi, As i have gone through

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.

admin
Offline
Joined: 2010-09-06
Your template isn't a template

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.

troysev
Offline
Joined: 2011-02-11
RE

please provide working example reading xml.

and how it work with oracle ???
need to load xml from clob or blob

troysev
Offline
Joined: 2011-02-11
RE: Options

please may you attach example with reading xml, I would be great full if not difficult.

modman
Offline
Joined: 2010-11-25
XML and Oracle

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.