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?