title: XmlPullParser exception org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}content (position:START_TAG
body: <p>I'm trying to parse a <code>XML</code> which is a thirdparty response when I call certain API. Here is the response of the <code>XML</code>:</p>
  
  <pre><code>&lt;mojiva&gt;
  &lt;ad type="thirdparty" feed="xyz"&gt;
  &lt;url&gt;
    &lt;![CDATA[ ]]&gt;
  &lt;/url&gt;
  &lt;text&gt;
    &lt;![CDATA[ ]]&gt;
  &lt;/text&gt;
  &lt;track&gt;
    &lt;![CDATA[ ]]&gt;
  &lt;/track&gt;
  &lt;content&gt;
    &lt;script&gt; // Original sdk: "http://lp.mydas.mobi/custom/rich/common/js/mmisdk/mmsdk.min.js" &lt;/script&gt;
  &lt;/content&gt;
  &lt;/ad&gt;
  &lt;/mojiva&gt;
  </code></pre>
  
  <p>I'm trying to parse <code>&lt;content&gt;</code> tag, but I'm getting the following exception:</p>
  
  <pre><code>12-16 06:40:55.148: W/System.err(4089): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}content (position:START_TAG &lt;text&gt;@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)
  </code></pre>
  
  <p><strong>Updated Code:</strong>
  I'm parsing the <code>XML</code> using <code>XmlPullParser</code>. Here is what my code looks like:</p>
  
  <pre><code>public class AdPull {
  
  private static final String ns = null;
  List&lt;Entry&gt; all;
  InputStream is;
  
  public AdPull(InputStream open) {
      is = open;
  }
  
  public List&lt;Entry&gt; getData() {
      try {
          XmlPullParser parser = Xml.newPullParser();
          parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
          parser.setInput(is, null);
          parser.nextTag();
          all = (ArrayList&lt;Entry&gt;) readFeed(parser);
          for (int i = 0; i &lt; 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&lt;Entry&gt; readFeed(XmlPullParser parser)
          throws XmlPullParserException, IOException {
      List&lt;Entry&gt; entries = new ArrayList&lt;Entry&gt;();
      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;
          }
      }
  }
  }
  </code></pre>
  
  <p>I'm calling getData() from my MainActivity in async task:</p>
  
  <pre><code>class AsyncTaskRunner extends AsyncTask&lt;Void, List&lt;Entry&gt;, List&lt;Entry&gt;&gt; {
  
      @Override
      protected List&lt;Entry&gt; 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&lt;Entry&gt; list = ad.getData();
  
              return list;
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          return null;
      }
  }
  </code></pre>