Inserting custom xml for docx file

Hi,
We have a requirement to generate and populate a custom xml file that gets stored as part of the CustomXML associated with the word document. Our intended xml file would be a tree of tags, and associated values:


Sample text here
Sample text for n4



I’ve been trying to utilize the CustomXmlPart class and CustomXmlPartCollectionClasses, but I’m having trouble setting values in the way I want them to appear. Could you help out with sample code, or resources which do the same? Thanks

Hi Praneeth,


Thanks for your inquiry. I think you can achieve this by using the following code snippet:
Document doc = new Document();

String xmlData = “” +
“” +
" Sample text here" +
" Sample text for n4" +
“” +
“”;

CustomXmlPart myXml = new CustomXmlPart();
myXml.setData(xmlData.getBytes());
myXml.setId(“someId”);

doc.getCustomXmlParts().add(myXml);

doc.save(“C:\Temp\out.docx”);

I hope, this helps.

Best regards,

Hi Awais,

Thanks for the helpful reply - I think we have our requirements satisfied perfectly. We shall get back if we need anything more. Thanks.