XMLPullParser parser failed to parse “(??????) [????] ·” inside xml tag

孤街醉人 提交于 2019-12-07 20:19:14

问题


I am parsing following XMLPullParser with Jsoup

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

Here is my parsing code

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

When I call next text and it throws following exception..

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)

回答1:


Your xml isn't valid. &middot; is invalid reference for xml.

There are 5 predefined entity references in XML:

&lt; < less than

&gt; > greater than

&amp; & ampersand

&apos; ' apostrophe

&quot; " quotation mark

Updated

Simple use regex to replace all HTML characters from XML

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

this will replace &middot; by ""




回答2:


I was dealing with the same problem and I found super easy solution:

xmlPullParser.setFeature(Xml.FEATURE_RELAXED, true);



回答3:


Maybe you can do:

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

As this does not work with your implementation:

From apache commons-lang use the HTML conversion, as it seems to be HTML named entities:

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

Comment's question:

Replacing all indiscriminate:

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


来源:https://stackoverflow.com/questions/15110087/xmlpullparser-parser-failed-to-parse-middot-inside-xml-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!