Submitted by rustyspoone on Wed, 08/26/2009 - 22:03
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
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.Also interested
Hi,
I am also interested in using XML as datasource so I would love to see this feature implemented soon.
Regards
Bert
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.