gpt4 book ai didi

java - Xml解析嵌套标签返回null

转载 作者:行者123 更新时间:2023-12-01 12:31:07 24 4
gpt4 key购买 nike

您好,我是 XML 解析新手,并完成了 xml 解析的 google 教程。在教程中他们使用:https://stackoverflow.com/feeds/tag?tagnames=android&sort=newest

所以我想用有关帖子作者的信息来扩展它,我尝试了它,但无论我在作者标签(uri 标签和名称标签)中使用什么,它都会返回 null

发生“搜索标签”的我的文件

/**
* This class parses XML feeds from stackoverflow.com.
* Given an InputStream representation of a feed, it returns a List of entries,
* where each list element represents a single entry (post) in the XML feed.
*/
public class StackOverflowXmlParser {
private static final String ns = null;

// We don't use namespaces

public List<Entry> 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 List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();

parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("entry")) {
entries.add(readEntry(parser));
}

else {
skip(parser);
}
}
return entries;
}

// This class represents a single entry (post) in the XML feed.
// It includes the data members "title," "link," and "summary."
public static class Entry
{
public final String rating;
public final String title;
public final String link;
public final String author;

private Entry(String rating, String title, String link, String author)
{
this.rating = rating;
this.title = title;
this.link = link;
this.author = author;
}
}


// Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them
// off
// to their respective &quot;read&quot; methods for processing. Otherwise, skips the tag.
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "entry");
String rating = null;
String title = null;
String link = null;
String author = null;



while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}

String name = parser.getName();

if (name.equals("re:rank")){
rating = readRating_name(parser);
}

else if (name.equals("title")){
title = readTitle_name(parser);
}

else if (name.equals("id")){
link = readLink_name(parser);
}

else if (name.equals("name")){
author = readAuthor_name(parser);
}

else
{
skip(parser);
}
}
return new Entry(rating, title, link, author);
}

// Processes title tags in the feed.
private String readRating_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "re:rank");
String rating = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "re:rank");
return rating;
}

private String readTitle_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return title;
}

private String readLink_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "id");
String link = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "id");
return link;
}

private String readAuthor_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "name");
String author = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "name");
return author;
}

// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser)
throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}

// Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
// if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
// finds the matching END_TAG (as indicated by the value of "depth" being 0).
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;
}
}
}
}

我输出解析结果的类:

// Uploads XML from stackoverflow.com, parses it, and combines it with
// HTML markup. Returns HTML string.
private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException
{
InputStream stream = null;
StackOverflowXmlParser stackOverflowXmlParser = new StackOverflowXmlParser();
List<Entry> entries = null;




StringBuilder htmlString = new StringBuilder();

try {
stream = downloadUrl(urlString);
entries = stackOverflowXmlParser.parse(stream);
// Makes sure that the InputStream is closed after the app is
// finished using it.
}

finally
{
if (stream != null)
{
stream.close();
}
}


// Content section


for (Entry entry : entries)
{
// <a href="Link">Name link</a>

String question = getString(R.string.question);
String rating = getString(R.string.rating);
String author = getString(R.string.author);

// Question link + title

htmlString.append("<p>" + question + "<a href='" + entry.link + "'>" + entry.title + "</a><br />");
htmlString.append(rating + entry.rating + "<br />");
htmlString.append(author + entry.author + "</p>");




}
return htmlString.toString();
}

最佳答案

希望它对您有帮助,而不是使用 xml 解析,使用这种提供 json 数据的方法[易于编码]::

String xml = "xml here...";                
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read( xml );

您可以获取 json-lib -- http://json-lib.sourceforge.net/index.html

关于java - Xml解析嵌套标签返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25911585/

24 4 0