Page 1 of 1

How to replace text in header using getJAXBNodesViaXPath

PostPosted: Sat Jul 09, 2011 1:02 am
by gibba
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
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.

Re: How to replace text in header using getJAXBNodesViaXPath

PostPosted: Sat Jul 09, 2011 1:32 am
by jason
The problem is that your input document is a FlatOPC XML package, and the FlatOpcXmlImporter just uses a standard:

Code: Select all
               ((org.docx4j.openpackaging.parts.JaxbXmlPart)part).unmarshal( el );


That's now overridden in HeaderPart. See http://dev.plutext.org/trac/docx4j/changeset/1613

Re: How to replace text in header using getJAXBNodesViaXPath

PostPosted: Mon Jul 11, 2011 1:11 am
by jason
If you have any problems building from svn head, you could try http://dev.plutext.org/docx4j/docx4j-ni ... 110710.jar which incorporates this change.