Android XML Parsing Tutorial
Today i am going to discuss about parsing XML in android using DOM parser. Also i am demonstrating how to update ListView with parsed XML data.
The XML Structure
In this tutorial i’ll will be parsing the following XML file. You can get this xml file by clicking here
<? xml version = "1.0" encoding = "UTF-8" ?> < menu > < item > < id >1</ id > < name >Margherita</ name > < cost >155</ cost > < description >Single cheese topping</ description > </ item > < item > < id >2</ id > < name >Double Cheese Margherita</ name > < cost >225</ cost > < description >Loaded with Extra Cheese</ description > </ item > < item > < id >3</ id > < name >Fresh Veggie</ name > < cost >110</ cost > < description >Oninon and Crisp capsicum</ description > </ item > < item > < id >4</ id > < name >Peppy Paneer</ name > < cost >155</ cost > < description >Paneer, Crisp capsicum and Red pepper</ description > </ item > < item > < id >5</ id > < name >Mexican Green Wave</ name > < cost >445</ cost > < description >Onion, Crip capsicum, Tomato with mexican herb</ description > </ item > </ menu > |
Writing XML Parser Class
In your project create a class file and name it as XMLParser.java. The parser class mainly deals the following operations.
⇒ Getting XML content by making HTTP request
⇒ Parsing XML content and getting DOM element of xml.
⇒ Get each xml child element value by passing element node name.
⇒ Parsing XML content and getting DOM element of xml.
⇒ Get each xml child element value by passing element node name.
Getting XML content by making HTTP Request
This function will get XML by making an 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
After getting XML content we need to get the DOM element of the XML file. Below function will parse the XML content and will give you 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 "" ; } |
Usage
Following is the code snippet for handling xml operations. I am using xml parser class so far we built and looping through each xml element and getting the child data.
// All static variables // XML node keys static final String KEY_ITEM = "item" ; // parent node static final String KEY_NAME = "name" ; static final String KEY_COST = "cost" ; static final String KEY_DESC = "description" ; XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Document doc = parser.getDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName(KEY_ITEM); // looping through all item nodes <item> for ( int i = 0 ; i < nl.getLength(); i++) { String name = parser.getValue(e, KEY_NAME); // name child value String cost = parser.getValue(e, KEY_COST); // cost child value String description = parser.getValue(e, KEY_DESC); // description child value } |
Parsing XML data and updating into ListView
In my previous tutorial Android ListView Tutorial i explained how to create listview and updating with list data. Below i am implementing same listview but the list data i am updating is from parsed xml. This ListView has multiple sub text like name, cost and description.
public class AndroidXMLParsingActivity extends ListActivity { // All static variables // XML node keys static final String KEY_ITEM = "item" ; // parent node static final String KEY_ID = "id" ; static final String KEY_NAME = "name" ; static final String KEY_COST = "cost" ; static final String KEY_DESC = "description" ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Document doc = parser.getDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName(KEY_ITEM); // looping through all item nodes <item> for ( int i = 0 ; i < nl.getLength(); i++) { // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); Element e = (Element) nl.item(i); // adding each child node to HashMap key => value map.put(KEY_ID, parser.getValue(e, KEY_ID)); map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST)); map.put(KEY_DESC, parser.getValue(e, KEY_DESC)); // adding HashList to ArrayList menuItems.add(map); } // Adding menuItems to ListView ListAdapter adapter = new SimpleAdapter( this , menuItems, R.layout.list_item, new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int [] { R.id.name, R.id.desciption, R.id.cost }); setListAdapter(adapter); // selecting single ListView item ListView lv = getListView(); // listening to single listitem click lv.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // getting values from selected ListItem String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString(); String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString(); // Starting new intent Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity. class ); in.putExtra(KEY_NAME, name); in.putExtra(KEY_COST, cost); in.putExtra(KEY_DESC, description); startActivity(in); } }); } } |
Comments
Post a Comment