This post is now available at my new blog The IT Report:
http://theitreport.com/entries/programming/hands-on-with-android-xml-parsing
This post is now available at my new blog The IT Report:
http://theitreport.com/entries/programming/hands-on-with-android-xml-parsing
Hello:
Could you explain how can I do the parsing of the xml?..
The jar just contains the *.class and i can´t see it.
thanks..
/** 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);
tvScores.append(“\n”);
for (int j = 0; j < 2; j++) {
teams = (Element) games.getElementsByTagName(“team”).item(j);
teamName = ((Node) teams.getChildNodes().item(0))
.getNodeValue();
tvScores.append(teamName + “\t\t”);
scores = (Element) games.getElementsByTagName(“score”).item(j);
score = ((Node) scores.getChildNodes().item(0)).getNodeValue();
tvScores.append(score);
tvScores.append(“\n”);
}
}
}
Hi,
Could you please tell me the value of the String strURL which gives us the site address.
Thanx,
Nitin
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;
[...] Research more about this from here [...]