Page 1 of 1

Replace item1.xml?

PostPosted: Tue Aug 10, 2010 7:31 am
by dawns
Hopefully I didn't miss this in the examples, but I have a need to replace the item1.xml in a docx.

I want to create a template (.docx file), map the xml file to the content controls, creating repeats/conditionals/etc and save the template. The application I'm writing would then simply take that template and process it with my data (custom XML) and save to a docx as output. However, I am testing (using the CreateDocxWithCustomXml example) but it's not exactly what I'm looking for. I tried to force the partName, but it threw an exception.

Thanks.

Re: Replace item1.xml?

PostPosted: Tue Aug 10, 2010 8:06 pm
by jason
If you run the PartsList sample on your docx, you'll see the tree includes something like:

Code: Select all
    Part /word/document.xml [org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart]  containing JaxbElement:org.docx4j.wml.Document

        Part /customXml/item1.xml [org.docx4j.openpackaging.parts.CustomXmlDataStoragePart]

            Part /customXml/itemProps1.xml [org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart]  containing JaxbElement:org.docx4j.customXmlProperties.DatastoreItem


You could delete item1.xml and add replace it with a new part of the same name, but if you did this, you'd have to reattach itemProps1.xml as well.

The easiest approach is leave the structure intact, ie to keep your existing item1.xml part, and just replace its data.

CustomXmlDataStoragePart has methods:

Code: Select all
   public void setData(CustomXmlDataStorage data)

   public CustomXmlDataStorage getData() {


Use getData() to get the existing CustomXmlDataStorage, and then inject your data using either of:

Code: Select all

   public void setDocument(InputStream is) throws Docx4JException

   public void setDocument( org.w3c.dom.Document doc )



(Or, if it suited you better, you could use:

Code: Select all
public org.w3c.dom.Document getDocument() throws Docx4JException


and modify the existing document as you saw fit)

Let me know how you go.

Re: Replace item1.xml?

PostPosted: Wed Aug 11, 2010 7:51 am
by dawns
That worked well! Thank you very much for the speedy responses!!!