Search

Sunday, December 19, 2010

Reading a remote XML file with SAX

If you need to read an remote XML file you can use SAX, DOM or the parser you want. I have tested it with SAX approach.

The first thing you need is give permission to your app for accessing Internet. So, in AndroidManifest.xml, you should add the next line:

<uses-permission android:name="android.permission.INTERNET"/>


I will read the next location, in order to get the different currency values published by European Central Bank:


Let's begin with SAX example. I have made two classes: CurrencyXMLReader and CurrencyXMLHandler.

CurrencyXMLReader will be in charge of create SAXParserFactory,
CurrencyXMLHandler and read the remote XML file, as follows:

public class CurrencyXMLReader
{
   private String urlM;
   
   public CurrencyXMLReader( String theUrl) 
   {
      urlM = theUrl;
   }

   public CurrencyList obtainCurrencyList() throws ParserConfigurationException, 
                                                   SAXException, 
                                                   IOException
   {
      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
      SAXParser parser = parserFactory.newSAXParser();

      XMLReader xmlReader = parser.getXMLReader();
      CurrencyXMLHandler currHandler = new CurrencyXMLHandler();
      xmlReader.setContentHandler( currHandler);
      
      URL url = new URL( urlM);
      xmlReader.parse(new InputSource( url.openStream()));

      return currHandler.getCurrencyList();        
   }
}

where CurrencyList is a class wich will contain the list of currencies retrieved from Bank and the date of the data read. It will have no special interest for the purpose of this post.

XML file has the next content:

<gesmes:envelope>
   <gesmes:subject>Reference rates</gesmes:subject>
   <gesmes:sender>
      <gesmes:name>European Central Bank</gesmes:name>
   </gesmes:sender>

   <cube>
      <cube time="2010-12-17">
         <cube currency="USD" rate="1.3260"/>
         <cube currency="JPY" rate="111.26"/>
         <cube currency="BGN" rate="1.9558"/>
         ...
         ...
      </cube>
   </cube>
</gesmes:envelope>

And I am interested in the date of currency data and in the currencies and their values.

CurrencyXMLHandler will be in charge of process the data of XML we have read:

public class CurrencyXMLHandler extends DefaultHandler{
 
   private CurrencyList currencyListM = new CurrencyList();
 
   @Override
   public void startElement( String theNamespaceURI, 
                             String theLocalName,
                             String theQName, 
                             Attributes theAtts) throws SAXException 
   {
      if( theLocalName.equals("Cube"))
      {
         if( theAtts != null)
         {
            if( theAtts.getValue( "currency") != null)
            {
               String currency = theAtts.getValue("currency");
               String rate = theAtts.getValue( "rate");
               currencyListM.add( new CurrencyData( currency, rate));
            }
            else if( theAtts.getValue( "time") != null)
            {
               currencyListM.setDataDate( theAtts.getValue("time"));
            }
         }
      }
   }
 
   CurrencyList getCurrencyList()
   {
      return currencyListM;
   } 
}

13 comments:

  1. could you provide the source code, it is not working

    ReplyDelete
  2. Hi,

    what's is failing you? Do you want the whole example? I'm not showing here the whole code, only the most significant. So, data containers are not shown here. If you need it I will put it.

    Other possible problem you may have is with URL java class. In code I used _URL_ instead of URL because of javascript code style parser doesn't work with URL word.

    Thank you for reading us.

    Best regards,
    Jose.

    ReplyDelete
  3. Probably the best way can be that you make a rar with all the sources and upload them so that anyone can download the complete sample.

    ReplyDelete
  4. Considered that I am a rookie, it is too hard for me to understand without the whole example. Sorry to bother you

    ReplyDelete
  5. Could you show the whole code here?

    ReplyDelete
  6. Hi,

    here you have the whole example. Think that this is not a finished application. This is made for Android 2.2 and my paths in eclipse project aren't the same you may have. I hope you can resolve your doubts:

    http://www.2shared.com/file/_KOdMEFP/EuroCalculator.html

    BR. Jose.

    ReplyDelete
  7. No joking, your code is not working....
    anyway thanks for your help : )

    ReplyDelete
  8. I don't know where your problem is. Decribe it to me. The app is installed and working on my phone with the code I have supplied you.

    ReplyDelete
  9. i dont know, the code seems fine on eclipse, but once i try to run your program on a simulator (Google APIs 8), error occured...

    btw, you are really nice.

    ReplyDelete
  10. it shows "Source not found."

    ReplyDelete
  11. I think you're trying to use Android Google API's and they aren't necessary for this application and I haven't included them in it. So, in project properties check if you have selected it. Use right mouse button on project in package explorer, select properties->Android. In this screen you must have the list of Android API's. Select Android 2.2 instead of Google API's. This must fix your problem.

    ReplyDelete
  12. Hey. I am a coder, and often have similar problems in my work. In Google there are enough examples of open xml file https://wikiext.com/xml. There are many sites, on which there is an available explanation. You just need to clearly describe your problem.

    ReplyDelete