How to replace text in header using getJAXBNodesViaXPath
Posted: Sat Jul 09, 2011 1:02 am
Hello,
I'm trying to modify some text in the header using the fresh ocx4j 2.7 release with method getJAXBNodesViaXPath
However I receive the following exception:
Exception in thread "main" java.lang.NullPointerException
at org.docx4j.XmlUtils.getJAXBNodesViaXPath(XmlUtils.java:863)
at org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart.getJAXBNodesViaXPath(HeaderPart.java:119)
at jax4cTestReplace.main(jax4cTestReplace.java:44)
Does anybody know how to fix it?
Following my code
Many thanks indeed in advance.
I'm trying to modify some text in the header using the fresh ocx4j 2.7 release with method getJAXBNodesViaXPath
However I receive the following exception:
Exception in thread "main" java.lang.NullPointerException
at org.docx4j.XmlUtils.getJAXBNodesViaXPath(XmlUtils.java:863)
at org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart.getJAXBNodesViaXPath(HeaderPart.java:119)
at jax4cTestReplace.main(jax4cTestReplace.java:44)
Does anybody know how to fix it?
Following my code
- Code: Select all
public class jax4cTestReplace {
private static final String XPATH_SDT = "//w:sdt/w:sdtContent";
public static void main(final String[] args) throws IOException, Docx4JException, JAXBException
{
File inputfilepath = new File(args[0]);
File outputfilepath=new File (args[1]);
List<Object> list = null;
Iterator<Object> listSDT=null;
WordprocessingMLPackage wordMLPackage =
WordprocessingMLPackage.load(inputfilepath);
// MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
//Parts myparts=wordMLPackage.getParts();
List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
HeaderPart header = null;
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
if (hfp.getDefaultHeader() != null) {
header = hfp.getDefaultHeader();
}
}
list = header.getJAXBNodesViaXPath(XPATH_SDT, false);
listSDT=list.iterator();
//To save as Flat OPC XML:
//wordMLPackage.
wordMLPackage.save(outputfilepath);
}
}
Many thanks indeed in advance.