0

I have a XML coming from the API, it includes some ads to show up. Here is what XML looks like:

<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>

In this I two things to parse the <url> tag and the <img> tag for showing it properly inside my app. There is a case of CDATA coming inside both the tags.

How can I parse just the mentioned tags without CDATA to get url and show it. Any kind of help will be appreciated.

Updated code(Using 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;
    }
}
}

And here is my async task where I want to print the values of the XML feeds:

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;
    }
}

But, this is giving me error, whenever I'm passing the completeURL inside it The constructor parserPull(String) is undefined

What am I doing wrong here?

Anupam
  • 3,742
  • 18
  • 55
  • 87
  • Can you add your parsing code? – Hardik Joshi Dec 10 '13 at 10:34
  • @Prince Updated the question with code. Pleas have a look. – Anupam Dec 10 '13 at 10:44
  • @Anupam can you log your parsing results. i can give you a workign code but that won't help others coz i won't be pointing mistakes. is your parsing working fine? – Raghunandan Dec 10 '13 at 10:57
  • here http://stackoverflow.com/questions/20488449/trouble-with-xml-parsing-through-java/20491871#20491871 – KumailR Dec 10 '13 at 11:02
  • @Anupam use `XmlPullParser`. Follow http://developer.android.com/training/basics/network-ops/xml.html – Raghunandan Dec 10 '13 at 11:04
  • @Raghunandan I'm using `XmlPullParser`, I have my object class ready as `AdPull` from the link which you have provided. Now, the URL for the `XML` is in my `MainActivity` and I have to pass that URL to object class in `async` task which is in my `Activity` class. How can I achieve that, so that I can have desired contents. – Anupam Dec 10 '13 at 11:17
  • @Raghunandan I have my async task which looks something like this, but when I pass params in the object given error as `The constructor AdPull(String) is undefined`, What can be done here? My object looks like this `public AdPull(InputStream open) {` – Anupam Dec 10 '13 at 11:19
  • @Anupam show your xmlpullparser code and asynctask. – Raghunandan Dec 10 '13 at 11:20
  • @Raghunandan edited my previous comment please see that. – Anupam Dec 10 '13 at 11:21
  • @Anupam why not post the updated code by editing your post – Raghunandan Dec 10 '13 at 11:22
  • @Raghunandan Updated my question with both Adpull class and my async task. What am I doing wrong please suggest. – Anupam Dec 10 '13 at 11:39
  • @Anupam first you need to get the xml data from the server then convert it into inpustreamd and then pass the same to Adpull – Raghunandan Dec 10 '13 at 11:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42864/discussion-between-raghunandan-and-anupam) – Raghunandan Dec 10 '13 at 11:44

1 Answers1

1

Use a XmlPullParser

The constructor parserPull(String) is undefined 

public AdPull(InputStream open) expects a inputstream. You have wrong param for the constructor and name is also wrong.

In 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);
    }

}

Have the below in separate .java file

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

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

You do not call readFeed(XmlPullParser parser) in AdPull

In AdPull make the following changes

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;
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I want to give -1 !! sorry but why do you always give full code instead of showing issues/errors in OP's code? – Paresh Mayani Dec 10 '13 at 10:54
  • @PareshMayani i waited for op to change his code and then based on the issue answered. If you see anything wrong let me know or feel free to edit my post to make it better. Thanks your comment helps. – Raghunandan Dec 10 '13 at 12:27
  • 1
    Nope, answer looks good now with comments and detail wherever required. Cheers :) – Paresh Mayani Dec 10 '13 at 12:52