I am trying to parse an XML file that I have defined inside a folder named raw which itself is contained inside the res folder of my android application. I have essentially used the version from the Android Developers guide on how to process XML files. http://developer.android.com/training/basics/network-ops/xml.htmlCurrently the app will run and will locate the file, then run the parse method but the methods that are supposed to locate the tags that you want to extract data from seem to skip and as a result my array list of type Unit has in this instance a Unit with no name.Below is a copy of the class that parses the XML file followed by the XML file I am using to test.\[code\]public class UnitTableXMLParser extends AsyncTask<InputStream, Void, Void> { private final String ns = null; public ArrayList<Unit> parse(InputStream in) throws XmlPullParserException, IOException { try { XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(in, null); parser.nextTag(); return readFeed(parser); } finally { in.close(); } } private ArrayList<Unit> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, ns, "resources"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String name = parser.getName(); if (name.equals("unit")) { units.add(readUnit(parser)); } else { skip(parser); } } return units; } private Unit readUnit(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, ns, "unit"); String name = null; while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String parserName = parser.getName(); if (parserName.equals("name")) { name = readName(parser); } } return new Unit(name); } private String readName(XmlPullParser parser) throws IOException, XmlPullParserException { parser.require(XmlPullParser.START_TAG, ns, "name"); String name = readText(parser); parser.require(XmlPullParser.END_TAG, ns, "name"); return name; } 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; } } } @Override protected Void doInBackground(InputStream... params) { Log.d(TAG, "UnitTableXMLParser exectued"); try { parse(params[0]); } catch (IOException e) { Log.d("Background Thread IOException", e.getMessage()); } catch (XmlPullParserException e) { Log.d("Background Thread XmlPullParserException", e.getMessage()); } catch (Exception e) { Log.d("Exception", e.getMessage()); } return null; } @Override protected void onPostExecute(Void result) { xmlParsed = true; super.onPostExecute(result); }}\[/code\]And the XML file :
And the output :
Any ideas where I might be going wrong would be much appreciated, I have tried name.matches and contains for String matching yet that didn't work either.Cheers,Jamie