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