Reputation: 2135
How to add a node to text node in Java? I want to do this on a xml document
From:
<doc> My Long text </doc>
To:
<doc> My <b>Long</b> text </doc>
Thanks in advance
Upvotes: 2
Views: 593
Reputation: 3885
I believe that in such simple scenarios all you need to do, is to convert the xml into a string and perform a "string replace". This would involve much less code. But if you are restricted to working in xml format, then follow the instructions provided by @Stephen C, using any api e.g jdom
Upvotes: 0
Reputation: 718788
Assuming that you've parsed the XML and have a DOM. What you need to do is:
Delete the existing text node from the <doc>
element.
Insert the a text node consisting of the first part of the text into the <doc>
element.
Insert an element node for the <b>
element into the <doc>
element.
Insert the text content into the <b>
element.
Insert the a text node consisting of the remainder of the text into the <doc>
element.
Upvotes: 3