2

我已经完成了已经提出的问题,但它们都不是我问题的解决方案。

在运行了一些程序之后,我猜想一旦我们开始处理所需的节点,我们就不能跳过 xml 数据处理中的任何标签..(在这个代码片段中,我也发生了同样的情况)

我的代码是:

private List<Post> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException{
    parser.require(XmlPullParser.START_TAG, null, "rss");

    String title = new String();
    String link = new String();

    List<Post> posts = new ArrayList<Post>();
    Post post ;
    while(parser.next() != XmlPullParser.END_DOCUMENT) {
       // Log.d("tag name", parser.getName());
         if((parser.getEventType() == XmlPullParser.START_TAG) && parser.getName().equalsIgnoreCase("item")){

             Log.d("xml","reached to item");

            while((parser.nextTag() != XmlPullParser.END_TAG) && !parser.getName().equalsIgnoreCase("item")){
             //   Log.d("tag name", parser.getName());
                if(parser.getEventType() != XmlPullParser.START_TAG){
                    continue;
                }
                String name = parser.getName();
                if(name.equalsIgnoreCase("title")){
                    title = readTitle(parser);
                    Log.d("title", title);


                }else if(name.equalsIgnoreCase("link")){
                    link = readLink(parser);
                    Log.d("link", link);

                }else if(name.equalsIgnoreCase("comments")){
                    Log.d("pubdate","parsed");

                }else {
                    Log.d("xml", "beofre skip");
                    if(parser.getEventType()== XmlPullParser.START_TAG){
                        Log.d("xml", "true strt");
                    }
                    skip(parser);
                }


                Log.d("xml", "inside item tag..");
            }
            if(!title.isEmpty() && !link.isEmpty()){
                Log.d("xml ", "addig to list");
               posts.add(new Post(title, link));

                Log.d("xml ", "addig to list");
                title = new String();
                Log.d("xml ", "addig to list");
                link = new String();


            }

        }else{

             Log.d("xml", " in else");
             continue;
         }
        /*if (parser.getEventType() == XmlPullParser.START_DOCUMENT) {
            Log.d("start doc", parser.getName());
        } else if (parser.getEventType() == XmlPullParser.START_TAG) {
            Log.d("start tag", parser.getName());
        } else if (parser.getEventType() == XmlPullParser.END_TAG) {
            Log.d("end tag", parser.getName());
        }*/
    }
    return posts;
}


private String readLink(XmlPullParser parser) throws XmlPullParserException,IOException{
    parser.require(XmlPullParser.START_TAG, null , "link");
    String link = parser.nextText();              //readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "link");
    Log.d("xml", "read linkkk");
    return link;
}

private String readTitle(XmlPullParser parser) throws XmlPullParserException, IOException{
    parser.require(XmlPullParser.START_TAG, null, "title");
    String title = parser.nextText();         //readText(parser);
    parser.require(XmlPullParser.END_TAG, null, "title");
    Log.d("xml", "read titile");
    return title;
}

//for text extraction

private String readText(XmlPullParser parser) throws IOException, XmlPullParserException{
    String result ="";
    if(parser.next() == XmlPullParser.TEXT){
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
    if (parser.getEventType() != XmlPullParser.START_TAG) {
        Log.d("xml", "not starting tag");
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
        }
    }
}

这是该程序的堆栈跟踪:

11-20 13:59:01.736 2305-8026/example.harpreet D/feedparser: in parser method
11-20 13:59:01.756 2305-8026/example.harpreet D/new tag: from live input stream
11-20 13:59:04.359 2305-8026/example.harpreet D/xml:  in else
11-20 13:59:04.359 2305-8026/example.harpreet D/xml:  in else
(i skipped these " in else" stacktrace line for more clarity 
11-20 13:59:04.359 2305-8026/example.harpreet D/xml: reached to item
11-20 13:59:04.359 2305-8026/example.harpreet D/xml: read titile
11-20 13:59:04.359 2305-8026/example.harpreet D/title: MacBook Webcams can be used to covertly spy on people — With proof
11-20 13:59:04.359 2305-8026/example.harpreet D/xml: inside item tag..
11-20 13:59:04.359 2305-8026/example.harpreet D/xml: read linkkk
11-20 13:59:04.359 2305-8026/example.harpreet D/link: https://hack4friends.wordpress.com/2013/12/23/macbook-webcams-can-be-used-to-covertly-spy-on-people-with-proof/
11-20 13:59:04.359 2305-8026/example.harpreet D/xml: inside item tag..
11-20 13:59:04.359 2305-8026/example.harpreet D/pubdate: parsed
11-20 13:59:04.369 2305-8026/example.harpreet D/xml: inside item tag..
11-20 13:59:04.369 2305-8026/example.harpreet D/xml parse: org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT https://hack4fri...@32:132 in java.io.InputStreamReader@41b71e18) 
11-20 13:59:04.369 2305-8026/example.harpreet D/download service: status sending
11-20 13:59:04.379 2305-8026/example.harpreet D/DownloadService:  Service stopped

您还可以从此链接查看 xml 提要 = https://hack4friends.wordpress.com/feed/ 以供参考

4

0 回答 0