gpt4 book ai didi

java - 如何使用 XmlPullParser 解析复杂的 XML

转载 作者:行者123 更新时间:2023-12-02 00:00:49 24 4
gpt4 key购买 nike

大家好,我在 xml 解析方面遇到问题。我正在尝试使用 XmlPullParser 进行解析

XML 看起来像:

<lfm status="ok">
<topartists user="dailz" type="overall" page="1" perPage="50" totalPages="30" total="1493">
<artist rank="1">
<name>Oasis</name>
<url>http://www.last.fm/music/Oasis</url>
<image size="small">http://userserve-ak.last.fm/serve/34/44937531.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/44937531.jpg</image>
</artist>
<artist rank="2">
...
</artist>
</topartists>
</lfm>

这就是我正在尝试做的事情:

public class TopArtistsParser {

private static final String ns = null;
private static final String TAG = "TopArtistsParser";

public class Artist {

public final String rank;
public final String name;
public final String url;
public final String image;

private Artist(String rank, String name, String url, String image) {
this.rank = rank;
this.name = name;
this.url = url;
this.image = image;
}

}

public List<Artist> parse(InputStream in) throws XmlPullParserException,
IOException {
MyLog.d(TAG, "List<Artist> parse");
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<Artist> readFeed(XmlPullParser parser)
throws XmlPullParserException, IOException {

MyLog.d(TAG, "List<Artist> readFeed");
List<Artist> artistsList = new ArrayList<Artist>();

parser.require(XmlPullParser.START_TAG, ns, "lfm");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tag = parser.getName();
if (tag.equals("artist")) {
MyLog.d(TAG, "tag == 'artist'");
artistsList.add(readArtist(parser));
} else {
skip(parser);
}
}

return artistsList;
}

private Artist readArtist(XmlPullParser parser)
throws XmlPullParserException, IOException {

MyLog.d(TAG, "Artist readArtist");
parser.require(XmlPullParser.START_TAG, ns, "artist");

String rank = null;
String name = null;
String url = null;
String image = null;

while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tag = parser.getName();
if (tag.equals("name")) {
name = readName(parser);
MyLog.d(TAG, "tag == 'name': " + name);
} else if (tag.equals("url")) {
url = readUrl(parser);
MyLog.d(TAG, "tag == 'url': " + url);
} else if (tag.equals("image")) {
image = readImage(parser);
MyLog.d(TAG, "tag == 'image': " + image);
} else {
skip(parser);
}
}

return new Artist(rank, name, url, image);
}

private String readName(XmlPullParser parser) throws IOException,
XmlPullParserException {
MyLog.d(TAG, "readName");
parser.require(XmlPullParser.START_TAG, ns, "name");
String name = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "name");
return name;
}

private String readUrl(XmlPullParser parser) throws IOException,
XmlPullParserException {
MyLog.d(TAG, "readUrl");
parser.require(XmlPullParser.START_TAG, ns, "url");
String url = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "url");
return url;
}

private String readImage(XmlPullParser parser) throws IOException,
XmlPullParserException {
MyLog.d(TAG, "readImage");
parser.require(XmlPullParser.START_TAG, ns, "image");
String image = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "image");
return image;
}

private String readText(XmlPullParser parser) throws IOException,
XmlPullParserException {
MyLog.d(TAG, "readText");
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}

private void skip(XmlPullParser parser) throws XmlPullParserException,
IOException {

MyLog.d(TAG, "skip");

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;
}
}
}
}

我看了Parsing XML Data但这对我没有帮助。

你能帮我解析一下:排名、名称、网址、图片(尺寸大)吗?

最佳答案

我广告此代码和所有工作

<!-- language: java -->

while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tag = parser.getName();
if (tag.equals("topartists")) {
MyLog.d(TAG, "tag == " + tag);
tag = parser.getName();

} else if (tag.equals("artist")) {
MyLog.d(TAG, "tag == " + tag);
artistsList.add(readArtist(parser));
} else {
skip(parser);
}
}

关于java - 如何使用 XmlPullParser 解析复杂的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14890252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com