Page 1 of 1

Correctly inserting HTMLChunk in WordMLPackage

PostPosted: Thu Apr 12, 2012 8:53 pm
by Empirica
Hey, we got an inconsistency-problem when adding HTML chunks to an existing wordML Package. Indexes do not match, and we don't exactly know how to solve this. Let me explain this by code. This is basically a substitution-Problem.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
List<Object> texts = thisWordMLPackage.getMainDocumentPart()
                        .getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);

CTAltChunk chunk = ... //Chunk creation works

//does not work
texts.set(index, chunk);

//does not work either
((JAXBElement) texts.get(index)).setValue(chunks);

// DOES WORK, but the list we receive with the .getContent() method is not congruent with the texts list, i.o.w the index from XPATH is != index of DocumentPart and the inserting is wrong.
thisWordMLPackage.getMainDocumentPart().getContent().set((index),chunk)

 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


Is there a proper way to solve this? I am able to provide more information upon request, of course.

Thank you in advance,
Nico

Re: Correctly inserting HTMLChunk in WordMLPackage

PostPosted: Thu Apr 12, 2012 9:56 pm
by jason
I suspect you may be encountering a JAXB bug. Note the Javadoc on the docx4j method you are using:

Code: Select all
   /**
    * Fetch JAXB Nodes matching an XPath (for example "//w:p").
    *
    * If you have modified your JAXB objects (eg added or changed a
    * w:p paragraph), you need to update the association. The problem
    * is that this can only be done ONCE, owing to a bug in JAXB:
    * see https://jaxb.dev.java.net/issues/show_bug.cgi?id=459
    *
    * So this is left for you to choose to do via the refreshXmlFirst parameter.   
    *
    * @param xpathExpr
    * @param refreshXmlFirst
    * @return
    * @throws JAXBException
    */   
   public List<Object> getJAXBNodesViaXPath(String xpathExpr, boolean refreshXmlFirst)
         throws JAXBException {
      
      return XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement, xpathExpr, refreshXmlFirst);
   }   


A workaround is to marshall your content to a DOM Document, then:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                        binder = jc.createBinder();
                        jaxbElement =  (org.docx4j.wml.Document) binder.unmarshal( doc );
 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


Alternatively, use TraversalUtil instead.