2

i have xml link this
enter image description here

how to parser it to get name of emp and list img.i want use SAXparser or xmlPullparser but I don't know to do that.sorry for my bad english. please help me

1 Answer 1

1

Try using the XmlPullParser described here 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;
        }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

thank. Can you explant for me. because I newer in java
thank you so much. if I want activity will show img of emp I chose. how to do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.