Vjy
Vjy

Reputation: 2135

XML - Adding a Node to middle of a Text Node

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

Answers (2)

Hasan Fahim
Hasan Fahim

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

Stephen C
Stephen C

Reputation: 718788

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.

Upvotes: 3

Related Questions