Page 1 of 1

replace a paragraph by different others

PostPosted: Fri Jan 06, 2012 4:48 am
by nexor
Hello everybody,

maybe this question is quite easy to answer and a bit stupid but for now google was not quite able to help me.

I have a docx-file where I search for a certain paragraph with xpath containing a certain text.
Code: Select all
String xpath = "//w:p[w:r[w:t[contains(text(),'"+$example$+"')]]]";


The corresponding list just contains one entry which I want to replace like this:
Code: Select all
list.set(0, newContent);

(newContent is an Object containing new paragraphs. I got this Object from unmarshalling a string descriping my new paragraphs)

While investigating the internet I found an entry that if I'd change the result within the list it is directly altered within the document. So my idea was just to create a new Object an replace the on within the list. But if I save afterwards and try to open the docx-file nothing changed.

All in all my idea is to locate one paragraph within my docx-file and replace it by many others depending on how much results I got from a previous computing step.

I would be very grateful for every help!
Best wishes.

Re: replace a paragraph by different others

PostPosted: Fri Jan 06, 2012 9:34 am
by jason
That XPath will select a paragraph. You would then need to use getParent().getContent() to get the list it belongs to. That should work, but pls be aware of the JAXB limitations/bugs documented elsewhere:

1. getParent() doesn't work properly on a JAXBElement (but should be fine on a P object),
2. XPath binder stuff isn't updated every time your docx changes (so subsequent calls might not return the correct results if you have changed your docx).

Re: replace a paragraph by different others

PostPosted: Fri Jan 06, 2012 11:08 pm
by nexor
thanks for your help.

I don't think it is necessary to show changes. I just want to find this specific position and now want to replace it by x other paragraphs so why should I need to get the parent list? As you might notice I'm not quite familiar with docx4j and openXML so far.

Re: replace a paragraph by different others

PostPosted: Sat Jan 07, 2012 1:45 am
by nexor
maybe it would be best if anyone could give me an advice how to solve my problem independent from my first aproach.

I have the following situation:
* a docx-file with a specific tagged position: $$position$$
* at this position I want so insert a set of paragraphs following a specific hararchical structure

what would be the best aproach to achieve this?