Home > Enterprise >  fill XML file with special content
fill XML file with special content

Time:04-01

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:

  1. Iterate the DOM and find the element which has text
  2. Get the XPath of this element
  3. Replace the text with the Xpath you get in step 2
  • Related