2

我正在用 Jsoup 解析 XMLPullParser

<title>(??????) [????]0 BLACK LAGOON -???? &middot; ????- ?01-09?</title>
        <guid isPermaLink='true'>http://fenopy.eu/torrent/+black+lagoon+A+01+09+/OTcyOTA3Mw</guid>
        <pubDate>Wed, 27 Feb 2013 11:00:04 GMT</pubDate>
        <category>Anime</category>
        <link>http://fenopy.eu/torrent/+black+lagoon+A+01+09+/OTcyOTA3Mw</link>
        <enclosure url="http://fenopy.eu/torrent/-BLACK-LAGOON-01-09-/OTcyOTA3Mw==/download.torrent" length="569296173" type="application/x-bittorrent" />
        <description><![CDATA[ Category: Anime<br/>Size: 542.9 MB<br/>Ratio: 0 seeds, 3 leechers<br/> ]]></description>
        </item>

这是我的解析代码

int eventType = -1;

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                // at start of document: START_DOCUMENT
                case XmlPullParser.START_DOCUMENT:                      
                    break;

                // at start of a tag: START_TAG
                case XmlPullParser.START_TAG:
                    // get tag name
                    String tagName = parser.getName();


                    if (tagName.equalsIgnoreCase(TAG_TITLE))                            
                        String t = parser.nextText();

当我调用下一个文本并引发以下异常时..

org.xmlpull.v1.XmlPullParserException: unresolved: &middot; (position:TEXT (??????) [????] ...@36:59 in java.io.StringReader@40540698) 
at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
at org.kxml2.io.KXmlParser.error(KXmlParser.java:269)
at org.kxml2.io.KXmlParser.pushEntity(KXmlParser.java:818)
at org.kxml2.io.KXmlParser.pushText(KXmlParser.java:849)
at org.kxml2.io.KXmlParser.nextImpl(KXmlParser.java:354)
at org.kxml2.io.KXmlParser.next(KXmlParser.java:1378)
at org.kxml2.io.KXmlParser.nextText(KXmlParser.java:1432)
4

3 回答 3

7

我正在处理同样的问题,我找到了超级简单的解决方案:

xmlPullParser.setFeature(Xml.FEATURE_RELAXED, true);
于 2014-11-08T22:11:31.587 回答
1

您的 xml 无效。&middot;对 xml 的引用无效。

XML 中有 5 个预定义的实体引用:

&lt; < 小于

&gt; > 大于

&amp;& & 号

&apos; ' 撇号

&quot; " 引号

更新

简单使用正则表达式替换 XML 中的所有 HTML 字符

XMLString.replaceAll("(&[^\\s]+?;)", ""));

这将替换&middot;为“”

于 2013-02-27T11:04:08.043 回答
1

也许你可以这样做:

parser.setInput(...);
parser.defineEntityReplacementText("middot", "•");

因为这不适用于您的实现:

从 apache commons-lang 使用 HTML 转换,因为它似乎是 HTML 命名实体:

String xml = "<foo>Hello &middot; World!</foo>";
xml = StringEscapeUtils.unescapeHtml(xml);

评论问题:

替换所有不分青红皂白的:

String xml = "<...";

// Place all entities like "&middot;" in square brackets: "[middot]":
xml = xml.replaceAll("\\&(\\w+);", "[$1]");

// But not for the xml entities:
xml = xml.replaceAll("\\[(lt|gt|amp|quot|apos)\\]", "&$1;");
于 2013-02-27T11:15:48.250 回答