开发者

How to wrap XML text content with XML tag in Java?

I am traversing an xml document using w3c DOM and I need to wrap the substring of the text content inside an org.w3c.dom.Element with some tag based on some business logic.

For example, I want to turn

<title id="1">Java is a cool programming language</title>

into

<title id="1">Java is a <blah id="2">cool</blah> programming language</title>

I don't insist on using the w3c DOM library for my application so 开发者_StackOverflow社区any suggestions are welcome in terms of other libraries that could accomplish this.


All text in an XML document will be parsed by the parser.

But text inside a CDATA section will be ignored by the parser.

try this

<title id="1">Java is a <![CDATA[<blah id="2">cool</blah> ]]>programming language</title>


Typically you'd use &lt; and &gt; (and others) to construct such tags in your node values. These are so called 'entity references. See for example here for some info about them; Google/Bing/YourFavouriteSearchEngine for more details.

In your example, this would mean you'd use:

<title id="1">Java is a &lt;blah id="2"&gt;cool&lt;/blah&gt; programming language</title>

Cheers, Wim

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜