【问题标题】:unable to read CDATA in XML无法读取 XML 中的 CDATA
【发布时间】:2015-12-10 15:39:52
【问题描述】:

我无法使用 DOM4j 库读取 CDATA 内容

<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 由 XML 解析器处理。只需选择 元素的内容即可。

标签: java xml cdata dom4j


【解决方案1】:

你通过getStringValue获取CDATA内容

如果你只有一个音符元素:

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

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 2017-02-12
    • 2010-12-16
    • 2012-09-11
    • 2011-06-07
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    相关资源
    最近更新 更多