-3

I need detailed tutorial or example on Xml-Parsing In Android

Ahmed Aswani
  • 8,271
  • 7
  • 33
  • 54
Jaya
  • 31
  • 3
  • 9

3 Answers3

5

You can check out this post...

They would have nicely discussed all 3 kinds of parsers available in Android along with code samples

http://www.ibm.com/developerworks/opensource/library/x-android/index.html

DeRagan
  • 22,827
  • 6
  • 41
  • 50
2

You parse XML on Android just like you would do it in regular Java. You have both org.w3c.dom and org.xml.sax packages available in Android. Use the one that best fits your needs, there are plenty of tutorials for both of them available on the Internet.

Felix
  • 88,392
  • 43
  • 149
  • 167
-1
  1. Getting XML content by making HTTP Request

    public String getXmlFromUrl(String url) {
        String xml = null;
    
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
    
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }
    
  2. Parsing XML content and getting DOM element

    public Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
    
            DocumentBuilder db = dbf.newDocumentBuilder();
    
            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is);
    
            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }
                // return DOM
            return doc;
        }
    
  3. Get each xml child element value by passing element node name

    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }
    
    public final String getElementValue(Node elem) {
        Node child;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }
    
RamenChef
  • 5,557
  • 11
  • 31
  • 43
  • Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. – Andrew Barber Feb 20 '13 at 11:07