Page 1 of 1

Replace text in the header of a document

PostPosted: Wed Aug 22, 2012 10:02 pm
by sandra
Not shure If I should post this in docx4j or any XPath Forum. But referencing this code:

Code: Select all
public void replaceTitle(String headerString) throws JAXBException{
   
    String xpath = "//w:r[w:t[contains(text(),'#Dokumentname')]]";
    List<Object> list = mainDocumentPart.getJAXBNodesViaXPath(xpath, true);

    for (Object obj : list) {
       List<Object>  objContent=((R)obj).getContent();
        objContent.clear();           
        WordParagraph p = new WordParagraph(headerString);
        objContent.add(p.getP());
    }   
  }



How could I use this

Code: Select all
String xpath = "//w:r[w:t[contains(text(),'audith')]]";
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, true);


to find a String "#Dokumentname" in my Header? Cause I'm not shure how to change the search string xpath above.

Re: Replace text in the header of a document

PostPosted: Wed Aug 22, 2012 11:25 pm
by sandra
okay my header looks like this:

Code: Select all
<w:hdr [... much text ...]>
<w:p w:rsidRDefault="00F959A5" w:rsidR="00ED67B1">
   <w:pPr> [...]
   <w:r>
      <w:t>#Dokumentname</w:t>
   </w:r>
</w:p>
</w:hdr>


But I supposed that this should work:

Code: Select all
    String xpath = "//w:hdr[w:p[w:r[w:t[contains(.,'#Dokumentname')]]]]";
    List<Object> list;
   try {
      list = mainDocumentPart.getJAXBNodesViaXPath(xpath, true);
       for (Object obj : list) {
          List<Object>  objContent=((R)obj).getContent();
           objContent.clear();           
           WordParagraph p = new WordParagraph(headerString);
           objContent.add(p.getP());
       }
   } catch (JAXBException e) {
      log.debug("WordDocument - replaceTitle - "+e);      
   }


But sadly it doesn't... what am I doing wrong?

Re: Replace text in the header of a document

PostPosted: Wed Aug 22, 2012 11:56 pm
by jason
You can't nest square brackets like that in XPath.

Use one of the tools at http://stackoverflow.com/questions/6883 ... -selectors to get your XPath expression right

Re: Replace text in the header of a document

PostPosted: Thu Aug 23, 2012 12:29 am
by sandra
Many thanks for your answer.

With help from
http://chris.photobooks.com/xml/default.htm I now got this expression:

String xpath = "//w:hdr/w:p/w:r/w:t[contains(., '#Dokumentname')]";

And this references the t element with #Dokumentname inside. But still I'm getting an error while doing this:

Code: Select all
list = mainDocumentPart.getJAXBNodesViaXPath(xpath, true);


Code: Select all
2781  ERROR- sv.WordDocument  - WordDocument - Exception
java.lang.NullPointerException
   at org.docx4j.XmlUtils.getJAXBNodesViaXPath(XmlUtils.java:862)
   at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getJAXBNodesViaXPath(MainDocumentPart.java:174)
   at sv.WordDocument.replaceTitle(WordDocument.java:483)
   at sv.WordDocument.loadPartsFromTemplate(WordDocument.java:435)
   at sv.WordDocument.<init>(WordDocument.java:110)
   at sv.WordTester.main(WordTester.java:20)


Why?

Re: Replace text in the header of a document

PostPosted: Thu Aug 23, 2012 9:30 am
by jason
From Getting Started:

There are a few limitations however in the JAXB reference implementation:
• the xpath expressions are evaluated against the XML document as it was when first opened in docx4j. You can update the associated XML document once only, by passing true into getJAXBNodesViaXPath. Updating it again (with current JAXB 2.1.x or 2.2.x) will cause an error.
• For some objects,JAXB can’t get parent (with getParent)
• For some document, JAXB can’t set up the XPath
If these limitations are causing you problems, see Traversing below for a different approach.

If this doesn't explain your problem, feel free to attach the docx and I'll take a look. You are using 2.8.0, I take it?

cheers .. Jason

Re: Replace text in the header of a document

PostPosted: Thu Aug 23, 2012 7:07 pm
by sandra
So attached the docx, for your reference.
Actually I'm using 2.7 of docx4j because changing my local version make it necessary to change it on server - and I don't have the permission to do that.
Are there any new features relating to this issue with text replacement?

Re: Replace text in the header of a document

PostPosted: Mon Sep 10, 2012 1:14 am
by sandra
I still have the problem that I cant replace text in my document. Is there ANY way I can do it?