Working with XML - Tuong-Nguyen/Android GitHub Wiki

There are some library which can be used for working with XML:

  • DocumentBuilder: load the XML as DOM
  • SAXParser: parse XML as event
  • XmlPullParser.

Parse XML using XmlPullParser

<?xml version="1.0" encoding="utf-8" ?>
<products>
    <product>
        <productId>1</productId>
        <category>Shrubs</category>
        <name>Azalea</name>
        <instructions>Large double. Good grower, heavy bloomer. Early to mid-season, acid loving plants. Plant in moist well drained soil with pH of 4.0-5.5.</instructions>
        <price>15.99</price>
        <photo>california_snow.jpg</photo>
    </product>
</products>
public class MyPullParser {

    private static final String LOGTAG = "PULL_PARSER";

    private static final String NAME_TAG = "name";
    private static final String PRICE_TAG = "price";

    private Product mProduct = null;
    List<Product> mProducts = new ArrayList<>();
    private String mTag = null;

    public List<Product> parseXML(String xml) {

        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            StringReader reader = new StringReader(xml);
            xpp.setInput(reader);

            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    handleStartTag(xpp.getName());
                } else if (eventType == XmlPullParser.END_TAG) {
                    mTag = null;
                } else if (eventType == XmlPullParser.TEXT) {
                    handleText(xpp.getText());
                }
                eventType = xpp.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return mProducts;
    }

    private void handleText(String text) {
        String xmlText = text;
        if (mProduct != null && mTag != null) {
            if (mTag.equals(NAME_TAG)) {
                mProduct.setName(xmlText);
            }
            else if (mTag.equals(PRICE_TAG)) {
                double price = Double.parseDouble(xmlText);
                mProduct.setPrice(price);
            }
        }
    }

    private void handleStartTag(String name) {
        if (name.equals("product")) {
            mProduct = new Product();
            mProducts.add(mProduct);
        }
        else {
            mTag = name;
        }
    }
}
⚠️ **GitHub.com Fallback** ⚠️