I just use content control to complete word generation. While encounting this case: i want to copy some paragraphs serverl times from my template and bind different content to the corresponding position. i tried to use the bookmark to find wanted paragraph and copy to my document. Unluckily, the program meets exception with this:
ERROR org.docx4j.XmlUtils .xpath line 995 - javax.xml.xpath.XPathExpressionException
Exception in thread "main" java.lang.RuntimeException: javax.xml.xpath.XPathExpressionException
at org.docx4j.XmlUtils.xpath(XmlUtils.java:996)
at org.docx4j.XmlUtils.xpath(XmlUtils.java:969)
at org.docx4j.XmlUtils.getJAXBNodesViaXPath(XmlUtils.java:947)
at org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.getJAXBNodesViaXPath(JaxbXmlPartXPathAware.java:111)
at com.sap.hana.sdk.activation.TraverseFind.getP(TraverseFind.java:73)
at com.sap.hana.sdk.activation.TraverseFind.copyParagragh(TraverseFind.java:81)
at com.sap.hana.sdk.activation.TraverseFind.main(TraverseFind.java:49)
Caused by: javax.xml.xpath.XPathExpressionException
at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:295)
at org.docx4j.XmlUtils.xpath(XmlUtils.java:988)
... 6 more
Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
at org.apache.xpath.XPath.execute(XPath.java:365)
at org.apache.xpath.jaxp.XPathImpl.eval(XPathImpl.java:219)
at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:281)
... 7 more
Caused by: java.lang.NullPointerException
at org.apache.xpath.axes.AxesWalker.setRoot(AxesWalker.java:221)
at org.apache.xpath.axes.WalkingIterator.setRoot(WalkingIterator.java:157)
at org.apache.xpath.axes.NodeSequence.setRoot(NodeSequence.java:265)
at org.apache.xpath.axes.LocPathIterator.execute(LocPathIterator.java:212)
at org.apache.xpath.XPath.execute(XPath.java:337)
... 9 more
But if i don't execute the loop and just run the comment part, it is ok.
Can you kindly solve this problem? Thank you
my code:
- Code: Select all
package com.sap.hana.sdk.activation;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.docx4j.XmlUtils;
import org.docx4j.model.datastorage.BindingHandler;
import org.docx4j.model.datastorage.CustomXmlDataStorage;
import org.docx4j.model.datastorage.CustomXmlDataStorageImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.P;
public class TraverseFind {
public static void main(String[] args) throws Exception {
String inputfilepath = System.getProperty("user.dir") + "/template.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String itemId = CustomXmlUtils.getCustomXmlItemId(wordMLPackage).toLowerCase();
CustomXmlDataStoragePart customXmlDataStoragePart = (CustomXmlDataStoragePart) wordMLPackage.getCustomXmlDataStorageParts().get(
itemId);
CustomXmlDataStorage customXmlDataStorage = customXmlDataStoragePart.getData();
// P copy1 = copyParagragh(documentPart,"ViewName");
// addCopyParagragh(copy1, wordMLPackage.getMainDocumentPart());
//
//
//
// try {
// org.w3c.dom.Document customXml = customXmlDataStorage.getDocument();
// customXml.getElementsByTagName("ActivityObjectName").item(0).setTextContent("ddd");
//
// ((CustomXmlDataStorageImpl) customXmlDataStorage).setDocument(customXml);
// // execute binding
// BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
// } catch (Docx4JException e) {
// e.printStackTrace();
// }
for (int i = 0; i < 3; i++) {
P copy1 = copyParagragh(documentPart, "ViewName");
addCopyParagragh(copy1, wordMLPackage.getMainDocumentPart());
try {
org.w3c.dom.Document customXml = customXmlDataStorage.getDocument();
customXml.getElementsByTagName("ActivityObjectName").item(0).setTextContent("ddd"+i);
((CustomXmlDataStorageImpl) customXmlDataStorage).setDocument(customXml);
// execute binding
BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
} catch (Docx4JException e) {
e.printStackTrace();
}
i++;
}
String outputfilepath = System.getProperty("user.dir") + "/Copy.docx";
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}
private static P getP(String name, MainDocumentPart documentPart) throws JAXBException {
final String xpath = "//w:bookmarkStart[@w:name='" + name + "']/..";
List<Object> objects = documentPart.getJAXBNodesViaXPath(xpath, false);
return (org.docx4j.wml.P) XmlUtils.unwrap(objects.get(0));
}
private static P copyParagragh(MainDocumentPart documentPart, String placeholder) {
// 1. get the paragragh
P appointedParagragh = null;
try {
appointedParagragh = getP(placeholder, documentPart);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 2. copy the found paragragh to keep syling correct
P copy = (P) XmlUtils.deepCopy(appointedParagragh);
return copy;
}
private static void addCopyParagragh(P copy, ContentAccessor addTo) {
// add the paragraph to the document
addTo.getContent().add(copy);
}
}