Page 1 of 1

Docx4j not working for converting Flat OPC XML to docx

PostPosted: Wed Aug 31, 2016 2:09 am
by sandeeptol
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

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Wed Aug 31, 2016 2:11 am
by sandeeptol
attachments

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Wed Aug 31, 2016 10:46 am
by jason
You misunderstand what Flat OPC XML is!

Flat OPC XML is Microsoft's single file XML format equivalent of a zipped up OPC package.

You can see an outline of the XML, at https://github.com/plutext/docx4j/blob/ ... r.java#L82

What are you trying to do, at a high level?

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Tue Sep 06, 2016 4:33 pm
by sandeeptol
Hi Jason,
I am trying to convert XML to docx format.
I have XML generated from code but wnat it to be exported in Word document ( docx)

Simlar to link below. Though belo examples used modifying content , i just need to convert my own XML to docx
http://www.docx4java.org/svn/docx4j/tru ... nding.java

are there any solutions?

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Tue Sep 06, 2016 4:35 pm
by sandeeptol
Hi Jason,
I am trying to convert XML to docx format.
I have XML generated from code but wnat it to be exported in Word document ( docx)

Similar to link below. Though belo examples used modifying content , i just need to convert my own XML to docx
http://www.docx4java.org/svn/docx4j/tru ... nding.java

Are there any Solutions?

Rgds
Sandeep

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Tue Sep 06, 2016 9:34 pm
by jason
What does your XML look like?

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Wed Sep 14, 2016 12:59 am
by sandeeptol
The file is already attached invoice-data.xml in previous thread.?
can you please suggest a solution if you have

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Wed Sep 14, 2016 2:03 am
by jason
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
  <pkg:part pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:name="/_rels/.rels">
    <pkg:xmlData>
      <invoice>
        <customer>
          <name>Joe Bloggs</name>
        </customer>
        <items>
          <item>
            <name>apples</name>
            <price>$20</price>
          </item>
          <item>
            <name>bananas</name>
            <price>$30</price>
          </item>
          <item>
            <name>gas</name>
            <price>$40</price>
          </item>
        </items>
        <misc>
          <includeBankDetails>true</includeBankDetails>
          <wantspam>false</wantspam>
        </misc>
      </invoice>
    </pkg:xmlData>
  </pkg:part>
</pkg:package>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


That is your XML? Why would you wrap your XML in Microsoft's xmlPackage?

You might be able to put that in a CustomXML part, or it might confuse Word, since this is an odd things to do. I'd suggest you extract the contents from the pkg namespace, and use your XML in databound content controls.

This is basic bread and butter stuff for docx4j, nothing tricky here. Study the docx4j invoice example...

Re: Docx4j not working for converting Flat OPC XML to docx

PostPosted: Wed Sep 14, 2016 5:02 pm
by sandeeptol
Hi Jason, My XML is in below format and i am trying to convert to Docx format. The requirement is to generate document export mechanism for third party XML streaming
service which is in below format

?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<invoice>
<customer>
<name>Joe Bloggs</name>
</customer>
<items>
<item>
<name>apples</name>
<price>$20</price>
</item>
<item>
<name>bananas</name>
<price>$30</price>
</item>
<item>
<name>gas</name>
<price>$40</price>
</item>
</items>
<misc>
<includeBankDetails>true</includeBankDetails>
<wantspam>false</wantspam>
</misc>
</invoice>