Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid org.xmlpull.v1.XmlPullParserException while parsing XML
    primarykey
    data
    text
    <p>I have a situation where i call a web service and it returns me some HTML in an XML envelop. like:</p> <pre><code>&lt;xml version="1.0" cache="false"&gt; &lt;head/&gt; &lt;body&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;a href="link-to-prev-post"&gt; &lt;text color="red"&gt;&lt;&lt; Prev&lt;/text&gt; &lt;/a&gt; &lt;/td&gt; &lt;td&gt; &lt;a href="link-to-next-post"&gt; &lt;text color="red"&gt;| Next &gt;&gt;&lt;/text&gt; &lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/xml&gt; </code></pre> <p>I have to retrieve the <strong>link-to-prev-post</strong> &amp; <strong>link-to-next-post</strong> links.. so i can get more data through these links.</p> <p>I am using <strong>XmlPullParser</strong> to parse the above provided XML/HTML. To get the links for next/prev items, i am doing as follows:</p> <pre><code>if (xmlNodeName.equalsIgnoreCase("a")) { link = parser.getAttributeValue(null, "href"); } else if (xmlNodeName.equalsIgnoreCase("text")) { color = parser.getAttributeValue(null, "color"); if (color.equalsIgnoreCase("red") &amp;&amp; parser.getEventType() == XmlPullParser.START_TAG) { // check for next/prev blog entries links // but this parser.nextText() throws XmlPullParserException // i think because the nextText() returns &lt;&lt; Prev which the parser considers to be wrong String innerText = parser.nextText(); if (innerText.contains("&lt;&lt; Prev")) { blog.setPrevBlogItemsUrl(link); } else if (innerText.contains("Next &gt;&gt;")) { blog.setNextBlogItemsUrl(link); } } link = null; } } </code></pre> <p>It throws <strong>XmlPullParserException</strong> on execution of <strong>parser.nextText()</strong> ... and the value of the text element at this time is <strong>&lt;&lt; Prev</strong> .. i think it misunderstands this value with start tag because of the presence of <strong>&lt;&lt;</strong> in text..</p> <p>LogCat detail is:</p> <pre><code>04-08 18:32:09.827: W/System.err(688): org.xmlpull.v1.XmlPullParserException: precondition: START_TAG (position:END_TAG &lt;/text&gt;@9:2535 in java.io.InputStreamReader@44c6d0d8) 04-08 18:32:09.827: W/System.err(688): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:245) 04-08 18:32:09.827: W/System.err(688): at org.kxml2.io.KXmlParser.nextText(KXmlParser.java:1382) 04-08 18:32:09.827: W/System.err(688): at utilities.XMLParserHelper.parseBlogEntries(XMLParserHelper.java:139) 04-08 18:32:09.827: W/System.err(688): at serviceclients.PlayerSummaryAsyncTask.doInBackground(PlayerSummaryAsyncTask.java:68) 04-08 18:32:09.827: W/System.err(688): at serviceclients.PlayerSummaryAsyncTask.doInBackground(PlayerSummaryAsyncTask.java:1) 04-08 18:32:09.836: W/System.err(688): at android.os.AsyncTask$2.call(AsyncTask.java:185) 04-08 18:32:09.836: W/System.err(688): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 04-08 18:32:09.836: W/System.err(688): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 04-08 18:32:09.836: W/System.err(688): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 04-08 18:32:09.836: W/System.err(688): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 04-08 18:32:09.836: W/System.err(688): at java.lang.Thread.run(Thread.java:1096) </code></pre> <p>I hope i have clarified my problem.</p> <h1>Solution</h1> <p>Isnpired by <a href="https://stackoverflow.com/users/940321/martin-nordholts">Martin's</a> approach of converting the received data first to string, i managed my problem in a kind of mixed approach.</p> <ol> <li><p>Convert the received <strong>InputStream</strong>'s value to string and replaced the erroneous characters with * (or whatever you wish) : as follows</p> <pre><code>InputStreamReader isr = new InputStreamReader(serviceReturnedStream); BufferedReader br = new BufferedReader(isr); StringBuilder xmlAsString = new StringBuilder(512); String line; try { while ((line = br.readLine()) != null) { xmlAsString.append(line.replace("&lt;&lt;", "*").replace("&gt;&gt;", "*")); } } catch (IOException e) { e.printStackTrace(); } </code></pre></li> <li><p>Now i have a string which contains correct XML data (for my case), so just use the normal XmlPullParser to parse it instead of manually parsing it myself: </p> <pre><code>XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(false); XmlPullParser parser = factory.newPullParser(); parser.setInput(new StringReader(xmlAsString.toString())); </code></pre></li> </ol> <p>Hope this helps someone!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload