/** Generate or load the XML you want in your CustomXML part. */ public static org.w3c.dom.Document createCustomXmlDocument() { org.w3c.dom.Document domDoc = XmlUtils.neww3cDomDocument(); domDoc.appendChild(domDoc.createElement("someContent")); return domDoc; }
/** * Use an XSLT to alter the contents of this package. The output of the transformation must be * valid pck:package/pck:part format, as emitted by Word 2007. * * @param is * @param transformParameters * @throws Exception */ public void transform(Templates xslt, Map<String, Object> transformParameters) throws Exception { // Prepare in the input document FlatOpcXmlCreator worker = new FlatOpcXmlCreator(this); org.docx4j.xmlPackage.Package pkg = worker.get(); JAXBContext jc = Context.jcXmlPackage; Marshaller marshaller = jc.createMarshaller(); org.w3c.dom.Document doc = org.docx4j.XmlUtils.neww3cDomDocument(); marshaller.marshal(pkg, doc); // javax.xml.bind.util.JAXBResult result = new javax.xml.bind.util.JAXBResult(jc ); // Use constructor which takes Unmarshaller, rather than JAXBContext, // so we can set JaxbValidationEventHandler Unmarshaller u = jc.createUnmarshaller(); u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); javax.xml.bind.util.JAXBResult result = new javax.xml.bind.util.JAXBResult(u); // Perform the transformation org.docx4j.XmlUtils.transform(doc, xslt, transformParameters, result); // javax.xml.bind.JAXBElement je = (javax.xml.bind.JAXBElement)result.getResult(); // org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package)je.getValue(); org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package) XmlUtils.unwrap(result.getResult()); org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage = new org.docx4j.convert.in.FlatOpcXmlImporter(wmlPackageEl); ContentTypeManager ctm = new ContentTypeManager(); Part tmpDocPart = xmlPackage.getRawPart(ctm, "/word/document.xml", null); Part tmpStylesPart = xmlPackage.getRawPart(ctm, "/word/styles.xml", null); // This code assumes all the existing rels etc of // the existing main document part are still relevant. // if (wmlDocument==null) { // log.warn("Couldn't get main document part from package transform result!"); // } else { // this.getMainDocumentPart().setJaxbElement(wmlDocument); // } this.getMainDocumentPart() .setJaxbElement(((JaxbXmlPart<Document>) tmpDocPart).getJaxbElement()); // // if (wmlStyles==null) { // log.warn("Couldn't get style definitions part from package transform result!"); // } else { // this.getMainDocumentPart().getStyleDefinitionsPart().setJaxbElement(wmlStyles); // } this.getMainDocumentPart() .getStyleDefinitionsPart(true) .setJaxbElement(((JaxbXmlPart<Styles>) tmpStylesPart).getJaxbElement()); }
@Override public Node toNode(Model tableModel, TransformState transformState) throws TransformerException { org.w3c.dom.Document doc = XmlUtils.neww3cDomDocument(); return toNode(tableModel, transformState, doc); }