Best way to convert xml to have CDATA around text (in java)

前端 未结 4 582
[愿得一人]
[愿得一人] 2021-01-23 02:14

I have a weird requirement where I need to take some xml and re-write it so that the text nodes are wrapped in CDATA (this is for a client that won\'t allow normal escaping).

4条回答
  •  温柔的废话
    2021-01-23 02:49

    You can use XSLT to accomplish this, as long as a) all of the text you need to output is in elements, b) you only care about text nodes, c) you know the names of all the elements that contain text, and d) it's okay to emit any text in all of those output elements as CDATA. If all of those cases are true, then you could write an identity transform and add this element to it:

    
    

    See the W3C XSLT recommendation on this subject.

提交回复
热议问题