I need detailed tutorial or example on Xml-Parsing In Android
Asked
Active
Viewed 6,694 times
3 Answers
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
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; }
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; }
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

www.9android.net
- 289
- 3
- 3
-
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