gpt4 book ai didi

android如何解析xml获取属性和元素

转载 作者:行者123 更新时间:2023-11-29 01:23:25 25 4
gpt4 key购买 nike

我有这个 xml 链接
enter image description here

如何解析它以获取 emp 的名称和列表 img。我想使用 SAXparser 或 xmlPullparser,但我不知道该怎么做。抱歉我的英语不好。请帮助我

最佳答案

尝试使用此处描述的 XmlPullParser http://developer.android.com/training/basics/network-ops/xml.html

public class EmployeeXmlParser {
private static final String ns = null;

// We don't use namespaces

public Detail 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 readDetail(parser);
} finally {
in.close();
}
}

private Detail readDetail(XmlPullParser parser) throws XmlPullParserException, IOException {
Detail detail = new Detail();

parser.require(XmlPullParser.START_TAG, ns, "employees");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tag = parser.getName();
// Starts by looking for the entry tag
if (tag.equals("detail")) {
detail.entries = readEmployees(parser);
} else {
skip(parser);
}
}
return detail;
}

private List<Employee> readEmployees(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Employee> entries = new ArrayList<Employee>();

parser.require(XmlPullParser.START_TAG, ns, "detail");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tag = parser.getName();
// Starts by looking for the entry tag
if (tag.equals("emp")) {
entries.add(readEmployee(parser));
} else {
skip(parser);
}
}
return entries;
}


private Employee readEmployee(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "emp");
String name = parser.getAttributeValue(null, "name");
String link = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}

String tag = parser.getName();
if (tag.equals("img")) {
link = readText(parser);
} else {
skip(parser);
}
}
return new Employee(name, link);
}


// For the tags name 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;
}
}
}

public static class Detail {
public List<Employee> entries = new ArrayList<Employee>();
}

public static class Employee {
public final String name;
public final String link;

private Employee(String name, String link) {
this.name = name;
this.link = link;
}
}

}

关于android如何解析xml获取属性和元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35447452/

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