Since Android Applications are written in Java, anything you can do with Java you can pretty much do on a droid. Of course there are limitations, as well as classes that are exclusive to the android SDK. In this example we will parse XML in an android application using POJO (plain old Java objects).
This example was created in Eclipse and is an Android 1.5 app (although it should work for any version).
If you are new to android development, or Eclipse, you can get familiar with them here.
First of all, start a new droid application in Eclipse. The project that this example is based on is called DroidXML and is in the package:
package droid.xml;This article will use the Java parsing classes from Parsing XML with Java The only difference will be the class package.
Most of the hard work was done in the prelude to this article, so all that's really left to do is instantiate our FeedParser, parse the XML, and then send the results to a TextView.
Before we write any code there is one important thing we must do. In order for our android to access the XML API, it needs permission to use the internet. This is done by adding the following line to your app's AndroidManifest.xml file.
DroidXML.java
package droid.xml;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class DroidXML extends Activity {
TextView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = new TextView(this);
tv.setText(this.getData());
setContentView(tv);
}
public String getData(){
String results = "";
FeedParser fp = new FeedParser(
"http://www.zekkocho.com/api/japanesewordset.php?n=10");
List words = fp.parse();
for(Word w: words){
results += "********************************\n";
results += "Kanji: " + w.getKanji() + "\n";
results += "Hiragana: " + w.getKanji() + "\n";
for(String s: w.getDefinitions()){
results += "Definition: " + s + "\n";
}
}
return results;
}
}
The function getData strongly resembles the TestXML class from the previous article. Only this time instead of writing the results to a file, we are displaying them on the droid screen through a new TextView.
You might also be interested in





