使用CDATA标记进行XML解析

时间:2013-12-10 10:33:48

标签: android xml xml-parsing xmlpullparser

我有来自API的XML,其中包含一些要展示的广告。以下是XML的样子:

<mojiva>
<ad type="image">
<url>
<![CDATA[
http://google.com
]]>
</url>
<img type="image/png">
<![CDATA[
http://account.mobfox.com/activation_vad.php
]]>
</img>
<track>
<![CDATA[
http://ads.moceanads.com/2/img/c2d79d40-6182-11e3-8f06-a0369f167751
]]>
</track>
</ad>
</mojiva>

在这两件事中我要解析<url>标记和<img>标记,以便在我的应用中正确显示它。两个标签内都有CDATA个案例。

如何在没有CDATA的情况下解析所提到的标记以获取网址并显示它。 任何形式的帮助将不胜感激。

更新了代码(使用XMLPullParser):

public class AdPull {

private static final String demoURL = null;

public AdPull(InputStream open) {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(open, null);
        parser.nextTag();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private List<Entry> readFeed(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    List<Entry> entries = new ArrayList<Entry>();
    parser.require(XmlPullParser.START_TAG, demoURL, "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, demoURL, "ad");
    String url = null;
    String image = null;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("url")) {
            url = readUrl(parser);
        } else if (name.equals("img")) {
            image = readImage(parser);
        } else {
            skip(parser);
        }
    }
    return new Entry(url, image);
}

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

private String readImage(XmlPullParser parser) throws IOException,
        XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, demoURL, "img");
    String image = readText(parser);
    parser.require(XmlPullParser.END_TAG, demoURL, "img");
    return image;
}   

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 Entry {
    public final String adURL;
    public final String adImage;

    private Entry(String url, String image) {
        this.adURL = url;
        this.adImage = image;
    }
}
}

这是我的异步任务,我想要打印XML Feed的值:

class AsyncTaskRunner extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {

        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());
            new AdPull(is);

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

        return null;
    }
}

但是,每当我在其中传递completeURL时,这都会给我错误The constructor parserPull(String) is undefined

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

使用XmlPullParser

The constructor parserPull(String) is undefined 

public AdPull(InputStream open)期待输入流。你有错误的构造函数参数,名称也是错误的。

doInbackground

class AsyncTaskRunnerextends AsyncTask<Void ,List<Entry>,List<Entry>>{


    @Override
    protected List<Entry> doInBackground(Void... sUrl) {
            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;

    }

    @Override
    protected void onPostExecute(List<Entry> result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

}

将以下内容放在单独的.java文件中

   public class Entry {
    public final String adURL;
    public final String adImage;

     Entry(String url, String image) {
        this.adURL = url;
        this.adImage = image;
    }
}

您不能在AdPull中调用readFeed(XmlPullParser parser)

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 = readFeed(parser);

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