Page 1 of 1

Bind full XML document to docx

PostPosted: Fri Feb 10, 2012 6:51 am
by pgf
Hi,

Basic question: How can I bind an xml document/string to a docx ?

Detailed question:

I'm trying to create a docx from a another docx (template) that has several content controls. I've done the bindings with the Word 2007 Content Control Toolkit and I've been able to execute a small example based on the http://www.docx4java.org/trac/docx4j/browser/trunk/docx4j/src/main/java/org/docx4j/samples/CustomXmlBinding.java sample. However, on that sample you need to explicit set the content of a control via the setNodeValueAtXPath call. I don't want to deal with the controls and xml nodes and bind them "manually".

I have a xml document "<root><a>1</a><b>2</b><c>3</c></root>" and I want to bind the whole XML to the customPart/item1.xml (only customPart on the docx), that has content controls binded to /root[1]/a[1], ....

It seems a fairly simple question, but I've searched in the forum and the Getting Started guide and I didn't find a solution.

Thank you.

Re: Bind full XML document to docx

PostPosted: Fri Feb 10, 2012 7:07 am
by pgf
Ok, i've found out. Nothing like a good rubber duck debugging.

On that same example, just switch the line with the setNodeValueAtXPath call to something like

String s = "<root xmlns=\"http://yournamespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><a>1</a><b>2</b><c>3</c></root>";
customXmlDataStorage.setDocument(new ByteArrayInputStream(s.getBytes()));

Simple, as i said.

o/