如何在CDATA部分处理XML?

时间:2014-07-17 07:13:18

标签: java xml

我有这个XML部分:

<additionalChargeAttrs><![CDATA[
    <CPEChargeAttrs>
         <complexattributeinfo name =  "Total charge amount" value = "99.00000000"/>
         <complexattributeinfo name =  "Minimum number of installments" value = "3"/>  
         <complexattributeinfo name =  "Maximum number of installments" value = "3"/>  
         <complexattributeinfo name =  "Installment number" value = "1"/>
         <complexattributeinfo name =  "Prorated from date" value = "1960-01-01 19:50:00"/>
         <complexattributeinfo name =  "Prorated to date" value = "2020-12-31 19:50:00"/>
         <complexattributeinfo name =  "Bundle code" value = " "/>
         <complexattributeinfo name =  "Offer subtype" value = "P"/>
         <complexattributeinfo name =  "Installment eligibility" value = "Y"/>
         <complexattributeinfo name =  "NRC charge type" value = "Regular"/>
    </CPEChargeAttrs>
]]></additionalChargeAttrs>

我想使用JAVA代码将以下元素的值(在CDATA部分中)更改为3:

<complexattributeinfo name =  "Installment number" value = "1"/>

1 个答案:

答案 0 :(得分:1)

CDATA部分的内容未解析,这意味着它只是文本blob。您需要获取该文本blob并再次通过XML解析器运行它,更改属性值,将文档写入新字符串,并使用此新字符串替换原始CDATA内容。