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();
        }
    }
6
  • 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. Commented Jul 2, 2016 at 6: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 Commented Jul 2, 2016 at 6: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: s31.postimg.org/a71b719wr/Untitled.jpg Commented Jul 2, 2016 at 6:55
  • I think that is the problem. XmlPullParser.nextText() found a wrong node when there are some tags with the same name. This question maybe similar to yours? Commented Jul 3, 2016 at 9: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); } Commented Jul 6, 2016 at 2:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.