Showing posts with label Andriod. Show all posts
Showing posts with label Andriod. Show all posts

How to parse XML in Android

How to parse XML in Android

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.

Download Code

The XML Structure

In this tutorial i’ll will be parsing the following XML file. You can get this xml file by accessing http://api.androidhive.info/pizza/?format=xml

<?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.

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

static final String URL = "http://api.androidhive.info/pizza/?format=xml";

// 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

    static final String URL = "http://api.androidhive.info/pizza/?format=xml";

    // 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);

 

            }

        });

    }

}

android xml parsing and updating to listview

How to implement list view in Android application

How to implement list view in Android application

In this tutorial i will be demonstrating how to build simple android ListView. This article is about creating listview and launching new activity on selecting single list item.

Below is screenshot of final output

Android List View

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as AndroidListViewActivity.
2. Once the project is created open your main activity java file (in this case AndroidListViewActivity.java) and extend the class from ListActivity.

 

public class AndroidListViewActivity extends ListActivity { 3. Now we need a string resources file to store all list item labels. So create an XML file under values folder and name it as list_data.xml and paste the following code. ( Right Click on res/values ⇒ New ⇒ Android XML File) list_data.xml


    
        Adobe After Effects
        Adobe Bridge
        Adobe Dreamweaver
        Adobe Edge
        Adobe Fireworks
        Adobe Flash
        Adobe Photoshop
        Adobe Premiere
        Adobe Reader
        Adobe Illustrator
    

4. In ListView each list item will be an xml layout, so we can customize each list item. Create an XML file under res/layout folder and name it as list_item.xml and type the following code. This xml layout will be single list item row. ( Right Click on res/layout ⇒ New ⇒ Android XML File)




5. Now open your main activity java file (AndroidListViewActivity.java) and type the following code. In the following code i am importing all xml resources data and storing them in an Array. On the next step i am binding array to ListAdapter. AndroidListViewActivity.java
package com.onlinecodegeek.androidlistview;
 
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
 
public class AndroidListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         
        // storing string resources into Array
        String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
         
        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter(this, R.layout.list_item, R.id.label, adobe_products));
         
    }
}
6. Now run your project you can see listview with list of array items. But on clicking single list item you can see no action. So we need to start new activity on selecting single list item. Launching new Activity on selecting single list item In my previous article i had explained how to switch between screens. Here i am going to show single list item details in new screen. 7. Now create new activity class under src folder. Right Click on src/package folder ⇒ New ⇒ Class and name it as SingleListItem. (SingleListItem.java) 8. Open your AndroidListViewActivity.java and modify the code to following. In the following code i am getting the selected list item string(product name) and sending it to new Activity. AndroidListViewActivity.java
package com.onlinecodegeek.androidlistview;
 
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
 
public class AndroidListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         
        // storing string resources into Array
        String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
         
        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter(this, R.layout.list_item, R.id.label, adobe_products));
         
        ListView lv = getListView();
 
        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView parent, View view,
              int position, long id) {
               
              // selected item
              String product = ((TextView) view).getText().toString();
               
              // Launching new Activity on selecting single List Item
              Intent i = new Intent(getApplicationContext(), SingleListItem.class);
              // sending data to new activity
              i.putExtra("product", product);
              startActivity(i);
             
          }
        });
    }
}
Now in new activity we need to display the received from listview activity. 9. Create a new xml file under res/layout and name it as single_list_item_view.xml and type the following code. This XML file will be layout for SingleListItem.java single_list_item_view.xml


     

10. Now open your second activity file i.e SingleListItem.java and paste the following code. SingleListItem.java
package com.onlinecodegeek.androidlistview;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class SingleListItem extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);
         
        TextView txtProduct = (TextView) findViewById(R.id.product_label);
         
        Intent i = getIntent();
        // getting attached intent data
        String product = i.getStringExtra("product");
        // displaying selected product name
        txtProduct.setText(product);
         
    }
}
11. The final step is to add an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below AndroidManifest.xml


    
 
    
        
            
                
                
            
        
         
        
    

12. Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.

android listview single list item selected