XmlPullParser异常org.xmlpull.v1.XmlPullParserException:expected:START_TAG {null}内容(位置:START_TAG

时间:2013-12-16 06:51:41

标签: android xml xml-parsing xmlpullparser

当我调用某个API时,我试图解析XML这是第三方的响应。以下是XML

的回复
<mojiva>
<ad type="thirdparty" feed="xyz">
<url>
  <![CDATA[ ]]>
</url>
<text>
  <![CDATA[ ]]>
</text>
<track>
  <![CDATA[ ]]>
</track>
<content>
  <script> // Original sdk: "http://lp.mydas.mobi/custom/rich/common/js/mmisdk/mmsdk.min.js" </script>
</content>
</ad>
</mojiva>

我试图解析<content>代码,但我收到以下异常:

12-16 06:40:55.148: W/System.err(4089): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}content (position:START_TAG <text>@1:116 in     java.io.InputStreamReader@a6a613e0) 
12-16 06:40:55.148: W/System.err(4089):     at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)

更新代码: 我正在使用XML解析XmlPullParser。这是我的代码:

public class AdPull {

private static final String ns = null;
List<Entry> all;
InputStream is;

public AdPull(InputStream open) {
    is = open;
}

public List<Entry> getData() {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(is, null);
        parser.nextTag();
        all = (ArrayList<Entry>) readFeed(parser);
        for (int i = 0; i < all.size(); i++) {

            Log.i("........", "" + all.get(i).url);
            Log.i("........", "" + all.get(i).text);
            Log.i("........", "" + all.get(i).track);
            Log.i("........", "" + all.get(i).thirdPartyContent);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return all;
}

private List<Entry> readFeed(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    List<Entry> entries = new ArrayList<Entry>();
    parser.require(XmlPullParser.START_TAG, ns, "mojiva");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("ad")) {
            entries.add(readAd(parser));
        } else {
            skip(parser);
        }
    }
    return entries;
}

private Entry readAd(XmlPullParser parser) throws XmlPullParserException,
        IOException {
    parser.require(XmlPullParser.START_TAG, ns, "ad");

    String url = null;
    String text = null;
    String track = null;
    String content = null;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        Log.i("...............", name);
        if (name.equals("url")) {
            url = readUrl(parser);
        } else if (name.equals("text")) {
            text = readTexta(parser);
        } else if (name.equals("track")) {
            track = readTrack(parser);
        } else if (name.equals("content")) {
            content = readContent(parser);
        } else {
            skip(parser);
        }
    }
    return new Entry(url, text, track, content);
}

private String readContent(XmlPullParser parser) throws IOException,
        XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "content");
    String content = null;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("script")) {
            content = readScript(parser);
            Log.i(".......", content);
        } else {
            skip(parser);
        }
    }

    return content;
}

private String readScript(XmlPullParser parser) throws IOException,
        XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "script");
    String script = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "script");
    return script;
}

private String readUrl(XmlPullParser parser) throws IOException,
        XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "url");
    String url = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "url");
    return url;
}

private String readTexta(XmlPullParser parser) throws IOException,
        XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "text");
    String text = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "text");
    return text;
}

private String readTrack(XmlPullParser parser) throws IOException,
        XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "track");
    String track = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "track");
    return track;
}

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) {
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
        case XmlPullParser.END_TAG:
            depth--;
            break;
        case XmlPullParser.START_TAG:
            depth++;
            break;
        }
    }
}
}

我在异步任务中从我的MainActivity调用getData():

class AsyncTaskRunner extends AsyncTask<Void, List<Entry>, List<Entry>> {

    @Override
    protected List<Entry> doInBackground(Void... sUrl) {

        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httppost = new HttpGet(completeURL);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity ht = response.getEntity();
            String _respons = EntityUtils.toString(ht);
            InputStream is = new ByteArrayInputStream(_respons.getBytes());
            AdPull ad = new AdPull(is); // expects a input stream
            List<Entry> list = ad.getData();

            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

2 个答案:

答案 0 :(得分:0)

I can't read <content> tag from the response

在标记内容下,您有标记脚本。所以你还需要解析脚本标记

<content>
  <script> // Original sdk: "http://lp.mydas.mobi/custom/rich/common/js/mmisdk/mmsdk.min.js" </script>
</content>

示例:

我添加了用于测试的虚拟值。我把xml放在assests文件夹中,我解析了xml,如下所示

<mojiva>

    <ad
        feed="xyz"
        type="thirdparty" >

        <url>
                <![CDATA[http://google.com]]>
        </url>

        <text>
                <![CDATA[my text]]>
        </text>

        <track>
                 <![CDATA[my track]]>
        </track>

        <content>

            <script>
   "http://lp.mydas.mobi/custom/rich/common/js/mmisdk/mmsdk.min.js" 
            </script>
        </content>
    </ad>

</mojiva>

解析

InputStream is = MainActivity.this.getResources().getAssets().open("xmlparser.xml");
new Testing(is);

然后

    public class Testing {

    InputStream is;
    ArrayList<AllEntry> all;
    private static final String ns = null;
    public Testing(InputStream is) {
        // TODO Auto-generated constructor stub
        this.is = is;
        getData(); 
    }
    public List<AllEntry> getData() {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(is, null);
            parser.nextTag();
            all = (ArrayList<AllEntry>) readFeed(parser);
            for(int i=0;i<all.size();i++)
            {

                Log.i("........",""+all.get(i).url);
                Log.i("........",""+all.get(i).text);
                Log.i("........",""+all.get(i).track);
                Log.i("........",""+all.get(i).content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return all;
    }

    private List<AllEntry> readFeed(XmlPullParser parser)
            throws XmlPullParserException, IOException {
        List<AllEntry> entries = new ArrayList<AllEntry>();
       String value=null;
        parser.require(XmlPullParser.START_TAG, ns, "mojiva");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("ad")) {
                String relType = parser.getAttributeValue(null, "feed");  
                  if (relType.equals("xyz")){
                      value = parser.getAttributeValue(null, "type");
                   if(value.equals("thirdparty"))   
                    entries.add(readAd(parser));

              }

            } else {
                skip(parser);
            }
        }
        return entries;
    }

    private AllEntry readAd(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "ad");

        String url = null;
        String text = null;
        String track = null;
        String content = null;
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            Log.i("...............",name);
            if (name.equals("url")) {
                url = readUrl(parser);
            } else if (name.equals("text")) {
                text= readTexta(parser);
            } else if (name.equals("track")) {
               track = readTrack(parser);
            } 
            else if (name.equals("content")) {
                   content = readContent(parser);
                }else {
                skip(parser);
            }
        }
        return new AllEntry(url, text, track,content);
    }


    private String readContent(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "content");
         String content = null;
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("script")) {
             content=   readScript(parser);
             Log.i(".......",content);
            } else {
                skip(parser);
            }
        }  

        return content;
    }
    private String readScript(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "script");
        String url = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "script");
        return url;
    }
    private String readUrl(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "url");
        String url = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "url");
        return url;
    }

    private String readTexta(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "text");
        String text = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "text");
        return text;
    }
    private String readTrack(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "track");
        String track = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "track");
        return track;
    }
     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) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
            }
        }
    }
    public static class AllEntry{
        //public final String ad;
        public final String url;
        public final String text;
        public final String track;
        public final String content;

        private AllEntry(String url, String text,String track,String content) {
            this.url = url;
            this.text = text;
            this.track = track;
            this.content= content;

        }
    }
}

日志

12-16 03:04:02.956: I/........(1932):                 http://google.com
12-16 03:04:02.956: I/........(1932):         
12-16 03:04:02.956: I/........(1932):                 my text
12-16 03:04:02.956: I/........(1932):         
12-16 03:04:02.956: I/........(1932):                  my track
12-16 03:04:02.956: I/........(1932):         
12-16 03:04:02.966: I/........(1932):    "http://lp.mydas.mobi/custom/rich/common/js/mmisdk/mmsdk.min.js" 

答案 1 :(得分:0)

试试这个:

List<String> tags = new LinkedList<String>();
tags.add("url");
tags.add("text");
tags.add("track");
XmlResourceParser xml = getResources().getXml(R.xml.m);
try {
    for (int type = xml.next(); type != XmlResourceParser.END_DOCUMENT; type = xml.next()) {
        if (type == XmlResourceParser.START_TAG) {
            String name = xml.getName();
            if (tags.contains(name)) {
                type = xml.next();
                Log.d(TAG, "tag: " + name + ", value: " + xml.getText());
            }
        }
    }
} catch (XmlPullParserException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

这是res / xml / m.xml:

<mojiva>
<ad type="thirdparty" feed="xyz">
<url>
<![CDATA[some url]]>
</url>
<text>
<![CDATA[sample text]]>
</text>
<track>
<![CDATA[unknown track]]>
</track>
<content>
<script> // Original ble ble ble </script>
</content>
</ad>
</mojiva>