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.
我正在尝试解析我在一个名为raw的文件夹中定义的XML文件,该文件夹本身包含在我的android应用程序的res文件夹中。我基本上使用了Android开发者指南中关于如何处理XML文件的版本。
http://developer.android.com/training/basics/network-ops/xml.html
Currently 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.
下面是解析XML文件的类的副本,后跟我用来测试的XML文件。
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);
}
}
And the XML file :
和XML文件:
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.
任何我可能会出错的想法都会非常感激,我已经尝试过name.matches并且包含String匹配但是也没有用。
Cheers,
Jamie
1 个解决方案
#1
0
Just look through the nextTag() specification. It calls next() and return event if it is START_TAG or END_TAG otherwise throw a new XMLPullParserException. And no complanations how does it work if it meets text or doc-blocks. It's obvious that is the case. So, all your try- block is breaked and result is null. What an awful example and awful documentation gave developer.android.com! And I couldn't give you working answer- i'm new to Android...
只需查看nextTag()规范即可。它调用next()并返回事件,如果它是START_TAG或END_TAG,否则抛出一个新的XMLPullParserException。如果它遇到文本或文档块,那么它是如何工作的。事实很明显。因此,所有try-block都被破坏,结果为null。多么可怕的例子和糟糕的文档给了developer.android.com!我无法给你工作答案 - 我是Android新手......
#1
0
Just look through the nextTag() specification. It calls next() and return event if it is START_TAG or END_TAG otherwise throw a new XMLPullParserException. And no complanations how does it work if it meets text or doc-blocks. It's obvious that is the case. So, all your try- block is breaked and result is null. What an awful example and awful documentation gave developer.android.com! And I couldn't give you working answer- i'm new to Android...
只需查看nextTag()规范即可。它调用next()并返回事件,如果它是START_TAG或END_TAG,否则抛出一个新的XMLPullParserException。如果它遇到文本或文档块,那么它是如何工作的。事实很明显。因此,所有try-block都被破坏,结果为null。多么可怕的例子和糟糕的文档给了developer.android.com!我无法给你工作答案 - 我是Android新手......