I am working on a project in which I need to fill some xml file with a special content, for example let's say I have an example.xml
file :
<A>
<B>testB</B>
<C>
<D>testD</D>
<E>
<F>testF</F>
</E>
</C>
</A>
<G>testG</G>
expected output is :
<A>
<B>{{ A.B }}</B>
<C>
<D>{{ A.C.D }}</D>
<E>
<F>{{ A.C.E.F }}</F>
</E>
</C>
</A>
<G>{{ G }}</G>
Is there a java open source that can help me to solve this issue?
CodePudding user response:
Here are many java libs that can do this thing, please refer to this: https://www.baeldung.com/java-xml
From your example, seems you want to replace the text with the [xPath of the DOM element], so you need to do those steps:
- Iterate the DOM and find the element which has text
- Get the XPath of this element
- Replace the text with the Xpath you get in step 2