开发者

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:

  1. Delete the existing text node from the <doc> element.

  2. Insert the a text node consisting of the first part of the text into the <doc> element.

  3. Insert an element node for the <b> element into the <doc> element.

  4. Insert the text content into the <b> element.

  5. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜