Search code examples
javaxmljaxbdocx4j

DOCX4J XML how to get the "value" of a JAXBNodes


I want to bind some XML variable in a docx file (my var are in that pattern $varname$). So I use a function which return a List<Object> with the result of my search over the document.

String xpath = "//w:r[w:t[starts-with(text(), '$')]]";
List<Object> list = this.getDocumentPart().getJAXBNodesViaXPath(xpath, false);
if(!list.isEmpty()){
    for(int i = 0; i < list.size(); ++i){
            System.out.println(list.get(i).getClass());
    }
}

The result of the print is:

class org.docx4j.wml.R
class org.docx4j.wml.R
class org.docx4j.wml.R

But now I want to get the "value" ie $varname$ to compare it with a map (the key is the name of each variable) ?


Solution

  • I find solution:

    if(!list.isEmpty()){
        List<Object> listObjNode;
        for(int i = 0; i < list.size(); ++i){
            List<Object> r = ((R)list.get(i)).getContent();
            for(int j = 0; j < r.size(); ++j){
                javax.xml.bind.JAXBElement jaxb = (javax.xml.bind.JAXBElement)r.get(j);
                org.docx4j.wml.Text t = (org.docx4j.wml.Text)jaxb.getValue();
                System.out.println(t);
            }
        }
    }