我有XML链接这
android - Android如何解析XML获取属性和元素-LMLPHP

如何解析它以获得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;
        }
    }

}

07-24 09:28