0

I got problems when I try to get info from XML to a string array. Is works fine if I only have one if statment withinf the: if (eventType == XmlPullParser.START_TAG) {...}

The thing is tho, I want to diffrent things to my array, both "Allergen" and "Artikelbenamning". Not sure if the problem is in the doInBackground or in the onPostExecute.

Any suggestions on what i've done wrong? The app dosen't crash it just never present the data.

protected ArrayList<String> doInBackground(String[] params) {
        ArrayList<String>titles=new ArrayList<>();
        try {
            URL url = new URL(params[0]);
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (xpp.getName().equalsIgnoreCase("Allergen")) {
                        titles.add(xpp.nextText());
                        allergen=xpp.nextText();
                    }

                    if (xpp.getName().equalsIgnoreCase("Artikelbenamning")) {
                        titles.add(xpp.nextText());
                        namn=xpp.nextText();
                    }

                }
                eventType = xpp.next();

            }//end while

        }//end try
        catch(MalformedURLException exp){

        }
        catch(IOException exp){
        }
        catch(XmlPullParserException exp){
        }
        return titles;
    }
    protected void onPostExecute(ArrayList<String> titleResult){
        ListView listView=(ListView)findViewById(R.id.listView);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,titleResult);
        listView.setAdapter(adapter);
        if(dialog.isShowing()){
            dialog.dismiss();
        }
    }
  • Could you add some log into `catch` block to see whether there is a xml parse exception? And also log `xpp.getName()` in loop to see whether your equals condition filtered all nodes. – sakiM Jul 02 '16 at 06:19
  • I did the Log.d to debug my code I found that: D/debuggingTime: catch(XmlPullParserException exp) So the (XmlPullParserException exp) block is running but none of the others, not even the xpp.getName(). @sakiM –  Jul 02 '16 at 06:33
  • It works if I only take "Artikelbenamning" and not "Allergen" but not the other way around. Having a second look at the XML file I might think I know why. "Allergen" is a tag whiting another tag. Mabye this is the problem? This is a pic of the XML: https://s31.postimg.org/a71b719wr/Untitled.jpg –  Jul 02 '16 at 06:55
  • I think that is the problem. `XmlPullParser.nextText()` found a wrong node when there are some tags with the same name. [This question](http://stackoverflow.com/questions/23263086/xmlpullparser-get-data-from-tag-with-specific-attributevalue) maybe similar to yours? – sakiM Jul 03 '16 at 09:31
  • @sakiM Thanks :) Solved it with: if (xpp.getName().equalsIgnoreCase("Allergen")) { Log.d(TAG, "Allergen börjar"); xpp.nextTag(); allergen = xpp.nextText(); titles.add(allergen); } –  Jul 06 '16 at 02:12
  • oh... that's the classic problem in iterator usage. Iterator.nextItem() should be and can be called only once in loop block. – sakiM Jul 06 '16 at 06:22

0 Answers0