Hands on with Android: XML Parsing

10 12 2007

This post is now available at my new blog The IT Report:




5 responses

20 12 2007


Could you explain how can I do the parsing of the xml?..

The jar just contains the *.class and i can´t see it.


23 12 2007

/** Parse XML file and display scores in textview */
public void displayScores(String strURL) {
URL url;
URLConnection urlConn = null;

try {
url = new URL(strURL);
urlConn = url.openConnection();
} catch (IOException ioe) {
nm.notifyWithText(0, “Could not connect to scores server!”,
NotificationManager.LENGTH_SHORT, null);

Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConn.getInputStream());
} catch (IOException ioe) {
nm.notifyWithText(0, “Invalid XML format!!”,
NotificationManager.LENGTH_SHORT, null);
} catch (ParserConfigurationException pce) {
nm.notifyWithText(0, “Could not parse XML!”,
NotificationManager.LENGTH_SHORT, null);
} catch (SAXException se) {
nm.notifyWithText(0, “Could not parse XML!”,
NotificationManager.LENGTH_SHORT, null);

int gameSize = doc.getElementsByTagName(“game”).getLength();
Element games, teams, scores;
String teamName, score;

for (int i = 0; i < gameSize; i++) {
games = (Element) doc.getElementsByTagName(“game”).item(i);

for (int j = 0; j < 2; j++) {
teams = (Element) games.getElementsByTagName(“team”).item(j);
teamName = ((Node) teams.getChildNodes().item(0))
tvScores.append(teamName + “\t\t”);

scores = (Element) games.getElementsByTagName(“score”).item(j);
score = ((Node) scores.getChildNodes().item(0)).getNodeValue();

1 01 2008


Could you please tell me the value of the String strURL which gives us the site address.


21 01 2008

I am trying to use your code but am getting errors with the catch statements.

IOException, ParserConfigurationException, and SAXException all get errors that they “cannot be resolved to a type”.

Any ideas why? Are you declaring something up top i dont know about?

I import the following:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

10 03 2008
Blogs and RSS » Hands on with Android: XML Parsing sheehantu

[…] Research more about this from here […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: