Recent Posts
- November 2014 (1)
- January 2014 (1)
- October 2013 (1)
- May 2013 (1)
- February 2013 (2)
- October 2012 (1)
- August 2012 (1)
- July 2012 (4)
- June 2012 (13)
Blog Stats
- 36,360 hits
Write your code smartly
I am going to explain and show how you can easily parse a normal xml response using DOM Parsing concept. There are other parsing methods such as Pull Parsing and SAX Parsing and its on the choice of the developer in which way they want to parse.
Here is a simple XML response which can be a local file or accessible from the web.
<?xml version="1.0" encoding="utf-8"?> <settings> <woeid> <countrywoeid>2211096</countrywoeid> <countryname>Asia/Karachi</countryname> </woeid> <woeid> <countrywoeid>2211177</countrywoeid> <countryname>Asia/Karachi</countryname> </woeid> <woeid> <countrywoeid>2459115</countrywoeid> <countryname>America/New_York</countryname> </woeid> <woeid> <countrywoeid>1940345</countrywoeid> <countryname>Asia/Dubai</countryname> </woeid> <woeid> <countrywoeid>1105779</countrywoeid> <countryname>Australia/Sydney</countryname> </woeid> <woeid> <countrywoeid>2251945</countrywoeid> <countryname>Asia/Tehran</countryname> </woeid> </settings>
This is a local file inside the sdcard of the Android device. Using the code below we can access the XML file on the root of the sdcard.
InputStream filename = null;
//parse the local XML file
filename=new FileInputStream("/sdcard/weatherFile.xml");
InputSource is = new InputSource(filename);
Using the InputStream class we can easily set the location of the xml file in the sdcard as shown in the code above. InputSource uses the instance of the InputStream to utilize it in further parsing. Here is how we can use DocumentBuilder class to create a document of the xml response to parse its all nodes.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc=db.parse(new InputSource(is.getByteStream())); doc.getDocumentElement().normalize();
Document builder helps to create a tree structure with nodes in normalized form. The list of the nodes can be collected in the NodeList instance for further traversing.
NodeList nodeList = doc.getElementsByTagName("woeid");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
pointer++;
woeid=Integer.parseInt(fstElmnt.getElementsByTagName("countrywoeid").item(0).getTextContent());
timeZone=fstElmnt.getElementsByTagName("countryname").item(0).getTextContent();
}
You can collect the all the nodes text in an array or you can get in a string variable and upon your requirement, it is easily used further.
So here is how the final code looks
InputStream filename = null;
//parse the local XML file
filename=new FileInputStream("/sdcard/weatherFile.xml");
InputSource is = new InputSource(filename);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc=db.parse(new InputSource(is.getByteStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("woeid");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
pointer++;
woeid=Integer.parseInt(fstElmnt.getElementsByTagName("countrywoeid").item(0).getTextContent());
timeZone=fstElmnt.getElementsByTagName("countryname").item(0).getTextContent();
}