XML - Adding a Node to middle of a Text Node
How to add a node to 开发者_C百科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
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.
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
精彩评论