简体   繁体   中英

unable to read CDATA in XML

I'm not able to read CDATA content with DOM4j library

<notes>

<note>
<![CDATA[
    something
]]>
</note>

I'm trying something like:

if(element.getName().equalsIgnoreCase("notes")){
                List notes = element.elements();
                for (int inotes = 0; inotes<notes.size(); inotes++) {
                    String content = ""; // ??????
                }
            }

You get CDATA content by getStringValue

If you have just one note element:

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);

if you have multiple note tags, use:

List<Node> notes = docu.selectNodes( "//note", "." ,true);
for (Node nd: notes)
    {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM