无法在XML
问题描述:
阅读CDATA我不能够读取CDATA含量的dom4j库无法在XML
<notes>
<note>
<![CDATA[
something
]]>
</note>
我想是这样的:
if(element.getName().equalsIgnoreCase("notes")){
List notes = element.elements();
for (int inotes = 0; inotes<notes.size(); inotes++) {
String content = ""; // ??????
}
}
答
你得到CDATA内容by getStringValue
如果你只有一个音符元素:
String xml="<notes>\r\n" +
"\r\n" +
"<note>\r\n" +
"<![CDATA[\r\n" +
"qslkdfjqoisdèufç_rkjsdqfmlq_zds"+
"_èçé\"hc<<op<àç\">>>x>pciu\"éêù!x;%xkm<qknc"+
" something\r\n" +
"]]>\r\n" +
"</note></notes>";
// STRING => DOCUMENT
Document docu=DocumentHelper.parseText(xml);
// SELECT UNIQUE
Node nd=docu.selectSingleNode("//note");
// CONTENT
String value=nd.getStringValue();
System.out.println("VALUE="+value);
如果你有多个笔记标记
,使用方法:
List<Node> notes = docu.selectNodes("//note", "." ,true);
for (Node nd: notes)
{
CDATA由XML解析器处理。只需挑选出元素的内容即可。 –