直接上代码
public static void main(String[] args) {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<res>\n" +
"<userName>xxx</userName>\n" +
" <payListInfo>\n" +
" <OPType><![CDATA[ 5 ]]></OPType>\n" +
" </payListInfo>\n" +
"</res>";
//正则转化font写法
String patternFont = "<!\\[CDATA\\[(.*?)]]>";
// 创建 Pattern 对象
Pattern p2 = Pattern.compile(patternFont);
Matcher m2 = p2.matcher(xml);
if (m2.find( )) {
xml = m2.replaceAll("$1");
}
System.out.println(xml);
}
可以看到cdata包裹的值已经成功取出。