2015-12-10 29 views
0

閱讀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 = ""; // ?????? 
       } 
      } 
+0

CDATA由XML解析器處理。只需挑選出元素的內容即可。 –

回答

0

你得到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) 
    {