XML DOM Creating Mixed Content

317 views Asked by At

I need to insert an element in the middle of another element's text. Given here is "before" and "after" that I want

Before

<element1> This is content of element1 </element1>

After

<element1> This is <element2></element2> content of element1 </element1>

Any ideas how to achieve this....I am using Java Dom API.

1

There are 1 answers

2
Jon Skeet On

I suspect you'll need to remove the existing text node(s), and then create three new nodes:

  • The text before <element2>
  • <element2>
  • The text after <element2>

Add all of those to <element1> after removing the existing text node(s).