repalce xpath placeholders multiple times
Posted:
Wed Oct 03, 2012 5:01 am
by fachhoch
I want to replace several different xpath with paragraphs , first xpath works from second on it fails and as the commnet added for the method getJAXBNodesViaXPath(String xpathExpr, boolean refreshXmlFirst) suggests that, I am wondering how others are managing to repalce several different xpath in a document , please advice .
I uusing xpath palceholders to replace it with collection of paragraphs, please advice how can I repalce several xpath placeholders ?
Re: repalce xpath placeholders multiple times
Posted:
Wed Oct 03, 2012 5:25 am
by fachhoch
I am trying with this code
- Code: Select all
void repalceXpath(String xpath, List objects ) throws Exception{
try{
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
for(Object obj : list){
org.docx4j.wml.R r = (org.docx4j.wml.R) obj;
org.docx4j.wml.P parent = (org.docx4j.wml.P) r.getParent();
int index = documentPart.getContent().indexOf(parent);
documentPart.getContent().remove(parent);
for(Object object :objects){
documentPart.getContent().add(index,object);
index++;
}
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
the line int index = documentPart.getContent().indexOf(parent);
returns -1 when I call this method for second time with a different xpath.
Please advice .
Re: repalce xpath placeholders multiple times
Posted:
Wed Oct 03, 2012 5:33 pm
by jason
You could use a TraversalUtil based approach instead. See for example
data-binding-java-f16/xpath-with-word-2010-t1209.html
Re: repalce xpath placeholders multiple times
Posted:
Wed Oct 03, 2012 8:55 pm
by fachhoch
Thanks jason
I tried the code from
http://www.smartjava.org/content/create ... lly-docx4jIt worked and also an option to avoid the error.
Thanks