XML Parsing in Android
Xml stands for extensible markup language.There are types of parsing methods in android
1 SaxPullparser
2 DOM parser
Here we are using SaxPullparser ,because it is more efficient that any other parser in android.
Following is a example for XML parsing using SaxPullparser
CustomXML.java
package fortyonepost.com.cx;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import teste.com.teste.R;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class CustomXML extends Activity
{
//this object will store a reference to the Activity's resources
private Resources resources;
//this integer will store the XMLResourceParser events
private int event;
//a boolean that will tell if it's the tag we are looking for
private boolean isCorrectTag = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the application's resources
resources = this.getResources();
try
{
GetAttributes("tutorial");
GetAttributes("level1");
}
catch (XmlPullParserException e)
{
//Display a toast message.
Toast.makeText(this, "Could not retrieve the current parser event", Toast.LENGTH_SHORT);
}
catch (IOException e)
{
//Display a toast message.
Toast.makeText(this, "Could not read XML file", Toast.LENGTH_SHORT);
}
}
public void GetAttributes(String tagName) throws XmlPullParserException, IOException
{
//get a XmlResourceParser object to read contents of the 'custom.xml' file
XmlResourceParser parser = resources.getXml(R.xml.custom);
//get the initial parser event
event = parser.getEventType();
//while haven't reached the end of the XML file
while (event != XmlPullParser.END_DOCUMENT)
{
//get the type of the event
event = parser.getEventType();
//if parser finds a new tag and its name equals 'tagName'
if (event == XmlPullParser.START_TAG && parser.getName().contentEquals(tagName))
{
//print the attribute values at LogCat
//print the ID
Log.i("CustomXML", "id:" + parser.getIdAttribute());
//print the difficulty
float difficulty = parser.getAttributeFloatValue(null, "difficulty", 0.0f);
Log.i("CustomXML", "difficulty:" + Float.toString(difficulty));
//print the number of enemies
int enemies = parser.getAttributeIntValue(null, "enemies", 0);
Log.i("CustomXML", "enemies:" + Integer.toString(enemies));
//Found the tag, set the boolean to true
isCorrectTag = true;
}
else if (event == XmlPullParser.TEXT && isCorrectTag)
{
//Print the tag's content
Log.i("CustomXML", parser.getText());
//set isCorrectTag to false;
isCorrectTag = false;
//break the loop, we already found what we were looking for
break;
}
//go to the next tag
parser.next();
}
//Release resources associated with the parser
parser.close();
}
}
place a xml file in res-xm1-custom.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<tutorial id="level" difficulty="1.5" enemies="3">The tutorial.</tutorial>
<level1 id="level" difficulty="2.6" enemies="10">The first level.</level1>
</resources>
check the logcat in your emulator for output
No comments:
Post a Comment