Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to parse a XML which is a thirdparty response when I call certain API. Here is the response of the 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>

I'm trying to parse <content> tag, but I'm getting the following exception:

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)

Updated Code: I'm parsing the XML using XmlPullParser. Here is what my code looks like:

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

I'm calling getData() from my MainActivity in async task:

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;
    }
}
share|improve this question
    
Can some comment, why downvote? Before giving anyone downvote, explain the reason why? –  Anupam Dec 16 '13 at 7:04
    
else if (type.equals("thirdparty")) there is no tag by the name thirdparty –  Raghunandan Dec 16 '13 at 7:05
    
@Raghunandan type = parser.getAttributeValue(null, "type");. I'm getting type of the response from it and doing stuff accordingly. That is working fine I checked it. If type is thirdparty it is going inside, otherwise it is not. So, I think that can't be the issue here. –  Anupam Dec 16 '13 at 7:10
    
you need to parse script inside content –  Raghunandan Dec 16 '13 at 7:20
    
@Raghunandan I just want to print all the values inside <content>, for now I want to ignore <script> tag. Any comments from your side? –  Anupam Dec 16 '13 at 7:21

2 Answers 2

up vote 0 down vote accepted
I can't read <content> tag from the response

Under tag content you have tag script. So you need to parse script tag also

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

Example :

I added dummy values for testing. I put the xml in assests folder and i parsed the xml as below

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

And parsing

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

Then

    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;

        }
    }
}

Log

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" 
share|improve this answer
    
I have updated the code in my question. I have tried your solution but the thing is that it is not going inside the readContent() method, can you please see the updated code, and tell what am I missing? –  Anupam Dec 16 '13 at 14:36
    
@Anupam try the edited code now. it will work. See the log also. –  Raghunandan Dec 16 '13 at 15:00
    
@Anupam copy paste my code and try. meanwhile i will check what's wrong with your updated code. –  Raghunandan Dec 16 '13 at 15:03
    
@Anupam i tried your code also works. where do you call getData and use Log instead of System.out.println –  Raghunandan Dec 16 '13 at 15:08
1  
I have to only parse <content> tag not the <script> tag. Now it is working and solves my purpose. Marking your answer as accepted. –  Anupam Dec 17 '13 at 5:49

try this:

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

and this is 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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.