Dear All,
Anyone tried using docx4j for converting Flat OPC XML format (Custom XML) to docx?
I tried various links provided in old docx forums. but getting exceptions
Links
http://www.docx4java.org/svn/docx4j/tru ... nding.java
1)DOcx 4j Code
public static void main(String[] args) throws Exception {
String inputfilepath ="/docx4j-master/sample-docs/word/databinding/invoice-data.xml";
String outputfilepath = "/docx4j-master/sample-docs/word/databinding/sampleoutput.docx";
String itemId = "apples".toLowerCase();
// Load the Package
WordprocessingMLPackage wordMLPackage;
// First get the Flat OPC package from the File Object
FileInputStream is = new FileInputStream(inputfilepath);
Unmarshaller u = Context.jcXmlPackage.createUnmarshaller();
u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
javax.xml.bind.JAXBElement j = (javax.xml.bind.JAXBElement)u.unmarshal(is);
org.docx4j.xmlPackage.Package flatOpc = (org.docx4j.xmlPackage.Package)j.getValue();
System.out.println("unmarshalled ");
// Now convert it to a docx4j WordML Package
FlatOpcXmlImporter importer = new FlatOpcXmlImporter(flatOpc);
wordMLPackage = (WordprocessingMLPackage)importer.get();
wordMLPackage.save(new java.io.File(outputfilepath) );
System.out.println("..done");
}
2) Used attached invoice-data.xml
got below exception
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"invoice"). Expected elements are <{http://schemas.microsoft.com/office/2006/xmlPackage}package>,<{http://schemas.microsoft.com/office/2006/xmlPackage}xmlData>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
3) Modified invoice-data.xml (attached) with namespace package/part/xmldata
getting below exception
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Unrecognised package
at org.docx4j.convert.in.FlatOpcXmlImporter.get(FlatOpcXmlImporter.java:186)
at org.docx4j.samples.CustomXmlBinding.main(CustomXmlBinding.java:114)
Could someone help?
Rgds
Sandeep