【问题标题】:Error paring XML with apostrophy as '将带撇号的 XML 解析为 ' 时出错
【发布时间】:2012-08-13 09:22:56
【问题描述】:

我正在尝试使用 DOM 解析 XML 提要。当一个节点看起来像这样:

<title>&#039;Star Wars&#039; May the force live</title>

返回的 XML 只是 ' 然后解析器继续到下一个节点。

我是这样解析的:

NodeList list = node.getChildNodes();
    for (int j=0; j<list.getLength(); j++) {
        Node innerItem = list.item(j);
        String name = innerItem.getNodeName();

        if (name.equalsIgnoreCase("title")) {
            vo.setTitle(innerItem.getFirstChild().getNodeValue());
        }
    }

我该如何解决这个问题?该代码在 Android 4 中解析良好,但在 Android 2.3.3 中解析失败

【问题讨论】:

标签: android xml-parsing android-xml


【解决方案1】:

看起来您的问题与这个问题非常相似:android decoding html in xml file

HTML 字符似乎破坏了 DOM 解析器,因此无法从 xml 实体中获取字符串。有一个 HTML 函数可以解析字符串中的 HTML。

TextView tv;
String s = "<quote>&#039;Star Wars&#039; May the force live</quote>";
tv.setText(HTML.fromHtml(s));

输出:

"Star Wars" May the force live

但是似乎 DOM 没有得到要转换的字符串,所以下面的文章可能有用:Using XPATH and HTML Cleaner to parse HTML/XML

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多