Java 无法读取XML格式的CDATA
我无法使用DOM4j库读取CDATA内容Java 无法读取XML格式的CDATA,java,xml,cdata,dom4j,Java,Xml,Cdata,Dom4j,我无法使用DOM4j库读取CDATA内容 <notes> <note> <![CDATA[ something ]]> </note> 我正在尝试类似的东西: if(element.getName().equalsIgnoreCase("notes")){ List notes = element.elements(); for (int inotes = 0; in
<notes>
<note>
<![CDATA[
something
]]>
</note>
我正在尝试类似的东西:
if(element.getName().equalsIgnoreCase("notes")){
List notes = element.elements();
for (int inotes = 0; inotes<notes.size(); inotes++) {
String content = ""; // ??????
}
}
if(element.getName().equalsIgnoreCase(“notes”)){
列表注释=element.elements();
对于(int inotes=0;inotes您可以通过getStringValue获得CDATA内容
如果只有一个note元素:
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);
String xml=“\r\n”+
“\r\n”+
“\r\n”+
“>>x>pciu\”êù!x;%xkm\r\n”+
"";
//字符串=>文档
Document docu=DocumentHelper.parseText(xml);
//选择唯一的
Node nd=docu.selectSingleNode(//注意);
//内容
String value=nd.getStringValue();
System.out.println(“VALUE=“+VALUE”);
如果有多个注释标记,请使用:
List<Node> notes = docu.selectNodes( "//note", "." ,true);
for (Node nd: notes)
{
List notes=docu.selectNodes(//note“,”,true);
用于(节点nd:注释)
{
CDATA由XML解析器处理。只需选择元素的内容即可。