我试图从我的XML(包含重复的子元素)中获取顶级元素的列表
示例XML
<feed>
<folder name="subfolder1">
<file name="subfile1" />
<file name="subfile2" />
<folder name="subsubfolder1">
<file name="subsubfile1" />
<file name="subsubfile2" />
</folder>
</folder>
<folder name="subfolder2">
<file name="subfile1" />
<file name="subfile2" />
<folder name="subsubfolder1">
<file name="subsubfile1" />
<file name="subsubfile2" />
</folder>
</folder>
<file name="file1"/>
</feed>
我试图获取顶级元素的所有名称列表,例如
.subfolder1
.subfolder2
这是我的FeedReader ....
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();
Log.v("ab", "reed feed started");
parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String sectionName = parser.getName();
if(sectionName.equals("folder")) {
readFolder(parser);
}
}
return entries;
}
private void readFolder (XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "folder");
Log.v("ab", parser.getAttributeValue(null, "name"));
parser.require(XmlPullParser.END_TAG, ns, "folder");
}
这是我的LogCat ....
09-02 13:40:22.537 31736-31753/? V/ab reed feed started
09-02 13:40:22.537 31736-31753/? V/ab﹕ subfolder1
在找到文件夹元素的第一个实例后,任何人都可以帮助解决这个问题吗?
答案 0 :(得分:1)
您的上一个parser.require行看起来有问题:
parser.require(XmlPullParser.END_TAG, ns, "folder");
documentation你在这里做的是检查是否满足这些条件,如果没有,则抛出异常。因此,您目前处于刚刚阅读的“文件夹”开始标记中,并且您正在检查您是否位于“文件夹”结束标记中。由于您不在'folder'结束标记,因此parser.require将抛出异常。
如果删除该行,则应该让while循环继续运行,直到下一个文件夹开始标记。
编辑:这是一个完整的解决方案
我们需要一直持续到文档结尾,直到任何END_TAG,所以我将while循环修改为while (parser.next() != XmlPullParser.END_DOCUMENT)
,然后在readFolder方法调用之后添加一些额外的代码。如果我理解正确,您只能在名为“子文件夹”的文件夹之后跳过'子文件夹'。所以我已经包含了一个应该跳过这些的循环。
我也删除了parser.require行,因为我没有亲自看到需要,但这只是一种方法。
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();
Log.v("ab", "reed feed started");
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String sectionName = parser.getName();
if(sectionName.equals("folder")) {
readFolder(parser);
//these booleans will be used to help us skip the subfolders
boolean finishedTopLevelElement = false;
boolean unwantedSubFolderFound = false;
//this will loop until we are at a "folder" tag and have
//confirmed we have finished with the top level folder
while (!(("folder".equals(parser.getName())) && finishedTopLevelElement)){
parser.next();
//we only care about 'folder' tags, for anything else
//we keep looping
if ("folder".equals(parser.getName())){
if (parser.getEventType() == XmlPullParser.START_TAG){
//if we hit a folder start tag, we're at a sub-folder
unwantedSubFolderFound = true;
} else if (parser.getEventType() == XmlPullParser.END_TAG && !unwantedSubFolderFound){
//if we hit a 'folder' end tag and we've not got an unwanted subfolder then
//we're done, it's the end tag of the top-level folder
finishedTopLevelElement = true;
unwantedSubFolderFound = false;
} else {
//if it's a folder end tag and we HAVE previously found an unwanted sub folder start tag
//then we've successfully skipped that sub-folder and can keep looking
unwantedSubFolderFound = false;
}
}
}
}
}
return entries;
}
private void readFolder (XmlPullParser parser) throws XmlPullParserException, IOException {
Log.v("ab", parser.getAttributeValue(null, "name"));
}