Android 理解XmlPullParser示例
下午好 我正在学习Android开发者关于解析XML数据的课程。这一课我几乎都懂,但有一句话我想不通 但首先是示例:示例解析来自Stackoverflow的提要。但首先是提要:Stackoverflow提要中的每个帖子都显示为一个,每个帖子都有嵌套的标记Android 理解XmlPullParser示例,android,xml,parsing,xmlpullparser,Android,Xml,Parsing,Xmlpullparser,下午好 我正在学习Android开发者关于解析XML数据的课程。这一课我几乎都懂,但有一句话我想不通 但首先是示例:示例解析来自Stackoverflow的提要。但首先是提要:Stackoverflow提要中的每个帖子都显示为一个,每个帖子都有嵌套的标记 <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xm
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" ...">
<title type="text">newest questions tagged android - Stack Overflow</title>
...
<entry>
...
</entry>
<entry>
<id>http://stackoverflow.com/q/9439999</id>
<re:rank scheme="http://stackoverflow.com">0</re:rank>
<title type="text">Where is my data file?</title>
<category scheme="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest/tags" term="android"/>
<category scheme="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest/tags" term="file"/>
<author>
<name>cliff2310</name>
<uri>http://stackoverflow.com/users/1128925</uri>
</author>
<link rel="alternate" href="http://stackoverflow.com/questions/9439999/where-is-my-data-file" />
<published>2012-02-25T00:30:54Z</published>
<updated>2012-02-25T00:30:54Z</updated>
<summary type="html">
<p>I have an Application that requires a data file...</p>
</summary>
</entry>
<entry>
...
</entry>
...
</feed>
continue
表示“继续循环的下一次迭代”。因此,if
语句的意思是“如果这个项目不有趣,试试下一个”
package com.mo.xmlpullparsersample;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class StackOverflowParser {
// We are not using namespaces (i.e the fully qualified names) just the raw names
private static final String ns = null;
public List parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
// Specify the input stream and input encoding - which is false - so it
// will use XM: 1.0 endcoding by default)
parser.setInput(in, null);
// Move the parser to the next available tag
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List entries = new ArrayList();
// Test if the parser is where we require it to be - i.e the current event should be a
// start-tag whose name is feed - i.e <feed>=. Stackoverflow's feed's first event is <feed>.
// If the test fails then an exception is thrown.
parser.require(XmlPullParser.START_TAG, ns, "feed");
// While the parser has not reached the corresponding end-tag - i.e </feed>
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
// parser.getName() returns the name of the current tag for start-tag or end-tag events
String name = parser.getName();
// tests if the name = "entry" if yes, then read the entry.
if (name.equals("entry")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
}