Android的org.xmlpull.v1.XmlPullParserException解析XML [英] Android org.xmlpull.v1.XmlPullParserException while parsing XML

查看:1353
本文介绍了Android的org.xmlpull.v1.XmlPullParserException解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我调用Web服务,它返回我在一个XML信封一些HTML。这样的:

I have a situation where i call a web service and it returns me some HTML in an XML envelop. like:

<xml version="1.0" cache="false">
<head/>
<body>
<table>
<tr>
   <td>
        <a href="link-to-prev-post">
           <text color="red"><< Prev</text>
        </a>
   </td>
   <td>
        <a href="link-to-next-post">
           <text color="red">| Next >></text>
        </a>
   </td>
</tr>
</table>
</body>
</xml>

我需要检索的链接于─preV-交&放大器; 链接到下一个岗位链接..所以我可以通过这些链接获取更多的数据。

I have to retrieve the link-to-prev-post & link-to-next-post links.. so i can get more data through these links.

我使用的 XmlPullParser 解析上面提供的XML / HTML。为了得到下一个/ $ P $光伏项目的链接,我做如下:

I am using XmlPullParser to parse the above provided XML/HTML. To get the links for next/prev items, i am doing as follows:

if (xmlNodeName.equalsIgnoreCase("a")) {
                link = parser.getAttributeValue(null, "href");

            } else if (xmlNodeName.equalsIgnoreCase("text")) {
                color = parser.getAttributeValue(null, "color");

                if (color.equalsIgnoreCase("red") && parser.getEventType() == XmlPullParser.START_TAG) {
                        // check for next/prev blog entries links
                        // but this parser.nextText() throws XmlPullParserException
                        // i think because the nextText() returns << Prev which the parser considers to be wrong
                        String innerText = parser.nextText();
                        if (innerText.contains("<< Prev")) {
                            blog.setPrevBlogItemsUrl(link);                             
                        } else if (innerText.contains("Next >>")) {
                            blog.setNextBlogItemsUrl(link);
                        }
                    }

                    link = null;
                }
            }

它抛出的 XmlPullParserException 在执行 parser.nextText() ...和文本元素此时的值为&LT;&LT; $ P $光伏 ..我想这是因为误解的&LT的presence该值开始标记; 文本..

It throws XmlPullParserException on execution of parser.nextText() ... and the value of the text element at this time is << Prev .. i think it misunderstands this value with start tag because of the presence of << in text..

LogCat中的细节是:

LogCat detail is:

04-08 18:32:09.827: W/System.err(688): org.xmlpull.v1.XmlPullParserException: precondition: START_TAG (position:END_TAG </text>@9:2535 in java.io.InputStreamReader@44c6d0d8) 
04-08 18:32:09.827: W/System.err(688):  at org.kxml2.io.KXmlParser.exception(KXmlParser.java:245)
04-08 18:32:09.827: W/System.err(688):  at org.kxml2.io.KXmlParser.nextText(KXmlParser.java:1382)
04-08 18:32:09.827: W/System.err(688):  at utilities.XMLParserHelper.parseBlogEntries(XMLParserHelper.java:139)
04-08 18:32:09.827: W/System.err(688):  at serviceclients.PlayerSummaryAsyncTask.doInBackground(PlayerSummaryAsyncTask.java:68)
04-08 18:32:09.827: W/System.err(688):  at serviceclients.PlayerSummaryAsyncTask.doInBackground(PlayerSummaryAsyncTask.java:1)
04-08 18:32:09.836: W/System.err(688):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-08 18:32:09.836: W/System.err(688):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-08 18:32:09.836: W/System.err(688):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-08 18:32:09.836: W/System.err(688):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-08 18:32:09.836: W/System.err(688):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-08 18:32:09.836: W/System.err(688):  at java.lang.Thread.run(Thread.java:1096)

我希望我已经澄清了我的问题。

I hope i have clarified my problem.

通过马丁的首先将接收到的数据串的方法Isnpired,我管理我的问题的一种混合的方法。

Isnpired by Martin's approach of converting the received data first to string, i managed my problem in a kind of mixed approach.

  1. 将接收到的的InputStream 的价值串带*号代替了错误的字符(或任何你想):如下:

  1. Convert the received InputStream's value to string and replaced the erroneous characters with * (or whatever you wish) : as follows

InputStreamReader isr = new InputStreamReader(serviceReturnedStream);

BufferedReader br = new BufferedReader(isr);
StringBuilder xmlAsString = new StringBuilder(512);
String line;
try {
    while ((line = br.readLine()) != null) {
        xmlAsString.append(line.replace("<<", "*").replace(">>", "*"));
    }
} catch (IOException e) {
    e.printStackTrace();
}

  • 现在我有一个包含正确的XML数据(我的情况)的字符串,因此就使用手动解析它自己的正常XmlPullParser解析它,而不是:

  • Now i have a string which contains correct XML data (for my case), so just use the normal XmlPullParser to parse it instead of manually parsing it myself:

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    
    factory.setNamespaceAware(false);
    
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(new StringReader(xmlAsString.toString()));
    

  • 希望这可以帮助别人!

    推荐答案

    是的,异常可能抛出,因为这是无效的XML作为每节的 2.4字符数据和标记在XML 1.0规范:

    Yes, the exception is probably thrown because that is invalid XML as per section 2.4 Character Data and Markup in the XML 1.0 specification:

    [...]左尖括号(小于)不得出现在[其]字面形式,[...]

    [...] the left angle bracket (<) MUST NOT appear in [its] literal form, [...]

    如果你把在Eclipse中的XML,Eclipse会抱怨的XML是无效的。如果你能解决Web服务,您应该修复生成的XML,通过使用实体引用,如&放大器; LT; 或使用的 CDATA

    If you put that XML in Eclipse, Eclipse will complain about the XML being invalid. If you are able to fix the web service, you should fix the generated XML, either by using entity references such as &lt; or by using CDATA.

    如果你没有权力,Web服务,我认为最简单的将是是手动解析了一些自定义的code,也许使用的常规EX pressions 的,这取决于你如何轻松的一般性要求有。

    If you have no power over the web service, I think the easiest will be to parse that manually with some custom code, perhaps using regular expressions, depending on how relaxed requirements of generality you have.

    下面是你如何可以解析上面的XML文件。请注意,你可能想提高这个code,使之更加普遍,但你应该有一些开始至少有:

    Here's how you could parse the XML file above. Note that you probably want to improve this code to make it more general, but you should have something to start with at least:

        // Read the XML into a StringBuilder so we can get get a Matcher for the
        // whole XML
        InputStream xmlResponseInputStream = // Get InputStream to XML somehow
        InputStreamReader isr = new InputStreamReader(xmlResponseInputStream);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder xmlAsString = new StringBuilder(512);
        String line;
        try {
            while ((line = br.readLine()) != null) {
                xmlAsString.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        // Look for links using a regex. Assume the first link is "Prev" and the
        // next link is "Next"
        Pattern hrefRegex = Pattern.compile("<a href=\"([^\"]*)\">");
        Matcher m = hrefRegex.matcher(xmlAsString);
        String linkToPrevPost = null;
        String linkToNextPost = null;
        while (m.find()) {
            String hrefValue = m.group(1);
            if (linkToPrevPost == null) {
                linkToPrevPost = hrefValue;
            } else {
                linkToNextPost = hrefValue;
            }
        }
    
        Log.i("Example", "'Prev' link = " + linkToPrevPost + 
                " 'Next' link = " + linkToNextPost);
    

    使用您的XML文件,将输出logcat的将是

    With your XML file, the output to logcat will be

    I/Example (12399): 'Prev' link = link-to-prev-post 'Next' link = link-to-next-post
    

    这篇关于Android的org.xmlpull.v1.XmlPullParserException解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆