Parsing XML in Android with SAX

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Parsing XML in Android with SAX

Postby danialhar788 » Thu Feb 23, 2012 12:27 pm

Android programs can engage in imported XML data from remote locations on the internet, or from our file system. The Java language provides a number of utilities for processing XML data, like the SAX and DOM parsers. In this particular tutorial we'll take advantage from the SAX parser. We'll process XML in the file held in an online location, while using the retrieved data to create interface elements with an Android application. You must have a chance to adapt the code to complement the needs of your family Android projects.

Prepare the XML Data

For those who have XML data you are coping with, technology-not just with this particular tutorial getting a few changes for the Java code. If you want to make a databases for that project, use Liquid XML Studio to produce your XML code. You'll be able to automate building the XML from an XSD (XML Schema Definition) for individuals who've one, or makes it in the table or tree. Alternatively, take advantage from the code editor to create your XML elements and qualities manually.

The Java code in this particular tutorial needs XML data using this structure:

<appdata>
<brand name="Lovely Products">
<product>Hat</product>
<product>Gloves</product>
</brand>
<brand name="Great Things">
<product>Table</product>
<product>Chair</product>
<product>Bed</product>
</brand>
</appdata>

You'll be able to adapt the Java code to fit your own XML, applying this just like a reference to the know the steps.

Create or Open an Android Project

For those who have an activity you are coping with, open it up up. Otherwise, create a new project. In the event you anticipate using XML data loaded on the web, add the following line for the project manifest file:

<uses-permission android:name="android.permission.INTERNET">
</uses-permission>

Create a Parsing Class

Create a new class within your project. The course will parse the imported XML data. Take advantage from the following outline, modifying it to complement the course title you are thinking about:

public class DataHandler extends DefaultHandler {
//class declaration goes here
}

The course stretches DefaultHandler, which gives the SAX parsing tools. Above these types declaration line, add the following import statement:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;

Make the Class Instance Variables and Constructor Method


Add the following code in your class declaration:

//list for imported product data
private ArrayList<TextView> theViews;
//string to track each entry
private String currBrand = "";
//flag to keep track of XML processing
private boolean isProduct = false;
//context for user interface
private Context theContext;
//constructor
public DataHandler(Context cont) {
super();
theViews = new ArrayList<TextView>();
theContext = cont;
}

The constructor method simply calls the strategy in the superclass, instantiates this list to hold the products as read in within the XML databases and instantiates the context object passed to make sure that we could create interface elements.

Add the traditional SAX Techniques

In your DefaultHandler class, the traditional SAX techniques will parse the XML data. They are instantly referred to as when the program encounters the start and finished in the document, the start and finished tags for elements, as well as the element content. Add the following method outlines after your constructor method:

//start of the XML document
public void startDocument () { Log.i("DataHandler", "Start of XML document"); }

//end of the XML document
public void endDocument () { Log.i("DataHandler", "End of XML document"); }

//opening element tag
public void startElement (String uri, String name, String qName, Attributes atts)
{
//handle the start of an element
}

//closing element tag
public void endElement (String uri, String name, String qName)
{
//handle the end of an element
}

//element content
public void characters (char ch[], int start, int length)
{
//process the element content
}

When the program encounters the start or finish in the document, we do not want it to accomplish anything, so simply output a standing update for the Android log for testing. We'll complete another three techniques next.

Process the start of Each Element

The startElement method can access the title in the element in the opening tag, plus any qualities it's. We'll create a string for each brand take into account the data, while using brand listed along with each product item for the brand. Add the following code in your startElement method:

//find out if the element is a brand
if(qName.equals("brand"))
{
//set product tag to false
isProduct = false;
//create View item for brand display
TextView brandView = new TextView(theContext);
brandView.setTextColor(Color.rgb(73, 136, 83));
//add the attribute value to the displayed text
String viewText = "Items from " + atts.getValue("name") + ":";
brandView.setText(viewText);
//add the new view to the list
theViews.add(brandView);
}
//the element is a product
else if(qName.equals("product"))
isProduct = true;

When the program encounters a brand name element, we create a new View item for your data you need to display for the brand, while using title and several informative text. If you process XML data with SAX, the program moves using the data in the linear fashion, so flags can help identify what reason behind the document the application reaches when for both executes.

Process the conclusion of each and every Element


Add the following in your endElement method:

if(qName.equals("brand"))
{
//create a View item for the products
TextView productView = new TextView(theContext);
productView.setTextColor(Color.rgb(192, 199, 95));
//display the compiled items
productView.setText(currBrand);
//add to the list
theViews.add(productView);
//reset the variable for future items
currBrand = "";
}

When the endElement method encounters the closing tag for just about any brand element, we add the string we have been building to a new View, adding this for the list and totally totally reset the string to empty when planning for an additional brand element.

Process the information of each and every Element

The figures method handles XML element content. This method must take account of the numerous types of whitespace that may are available in the XML data. In this particular application we simply ignore whitespace. Add the following in your figures method:

//string to store the character content
String currText = "";
//loop through the character array
for (int i=start; i<start+length; i++)
{
switch (ch[i]) {
case '\\':
break;
case '"':
break;
case '\n':
break;
case '\r':
break;
case '\t':
break;
default:
currText += ch[i];
break;
}
}
//prepare for the next item
if(isProduct && currText.length()>0)
currBrand += currText+"\n";

The strategy reaches be considered a character array while using element content within it, and so the code works through this array in the loop structure. We add each character with a string, creating the full element content, which we boost the current item text.

Give you the Data for the Application Context

The application needs ease of access parsed data, for example to exhibit it within the interface, so following a figures method, provide a public way other classes can call:

public ArrayList<TextView> getData()
{
//take care of SAX, input and parsing errors
try
{
//set the parsing driver
System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
//create a parser
SAXParserFactory parseFactory = SAXParserFactory.newInstance();
SAXParser xmlParser = parseFactory.newSAXParser();
//get an XML reader
XMLReader xmlIn = xmlParser.getXMLReader();
//instruct the app to use this object as the handler
xmlIn.setContentHandler(this);
//provide the name and location of the XML file **ALTER THIS FOR YOUR FILE**
URL xmlURL = new URL("http://mydomain.com/mydata.xml");
//open the connection and get an input stream
URLConnection xmlConn = xmlURL.openConnection();
InputStreamReader xmlStream = new InputStreamReader(xmlConn.getInputStream());
//build a buffered reader
BufferedReader xmlBuff = new BufferedReader(xmlStream);
//parse the data
xmlIn.parse(new InputSource(xmlBuff));
}
catch(SAXException se) { Log.e("AndroidTestsActivity", "SAX Error " + se.getMessage()); }
catch(IOException ie) { Log.e("AndroidTestsActivity", "Input Error " + ie.getMessage()); }
catch(Exception oe) { Log.e("AndroidTestsActivity", "Unspecified Error " + oe.getMessage()); }
//return the parsed product list
return theViews;
}

This code ought to be contained in the try block, with catch blocks for all the possible exception types. The code produces object installments of the appropriate SAX classes, opens a connection for the XML file on the web and lastly instructs the applying to parse the data. Be sure that you customize the URL code to enhance the title and positioning of the XML file. When the parsing is completed, this method returns this list of product data to a new class inside the application.

Call the Parsing Function and Display the data

To request the DefaultHandler class from your primary application Activity class, add this code inside the onCreate method:

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//get a reference to the layout
LayoutInflater inflater = getLayoutInflater();
LinearLayout mainLayout = (LinearLayout) inflater.inflate(R.layout.main,null);
try
{
//create an instance of the DefaultHandler class **ALTER THIS FOR YOUR CLASS NAME**
DataHandler handler = new DataHandler(getApplicationContext());
//get the string list by calling the public method
ArrayList<TextView> newViews = handler.getData();
//convert to an array
Object[] products = newViews.toArray();
//loop through the items, creating a View item for each
for(int i=0; i<products.length; i++)
{
//add the next View in the list
mainLayout.addView((TextView)products[i]);
}
}
catch(Exception pce) { Log.e("AndroidTestsActivity", "PCE "+pce.getMessage()); }

setContentView(mainLayout);
}

This code uses the data within the XML to exhibit numerous TextView items within the application interface. First, we have a reference to the the main layout, then create a new illustration showing the DefaultHandler class, passing the using Context - be sure that you customize the code to reflect the title of the class instead of "DataHandler". You have to call the getData method round the DefaultHandler object we created, to fetch and parse the data, finding it all of the TextView items. After changing this list to have an array, we add each TextView item for the layout. The try block takes proper proper care of any parsing exceptions. Finally the overall game sets its primary layout.

Add the following import claims with this particular code:

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

This really is really the resulting display running by having an Android emulator:


This application simply shows the parsed data items in the fundamental interface. On your own projects you need to use modern-day display techniques, for instance making the items interactive. Whatever that can be used for parsing the data, you must have a chance to take advantage of the code getting a few tweaks and inclusions in meet your needs.
danialhar788
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Feb 23, 2012 12:08 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests