0

I want to extract data from xml below. rate & currency works fine but i could not extract time.

<Cube> 
    <Cube time='2011-04-15'> 
        <Cube currency='USD' rate='1.4450'/> 
        <Cube currency='JPY' rate='120.37'/>
    </Cube> 

The code in startElement method

if (localName.equals("Cube")) {
            for (int i = 0; i < attributes.getLength(); i++) {
                if ((attributes.getLocalName(i)).equals("currency")) {
                    name = attributes.getValue(i);
                } else if ((attributes.getLocalName(i)).equals("time")) {
                    date = attributes.getValue(i);
                } 
                else if ((attributes.getLocalName(i)).equals("rate")) {
                    try {
                        rate = Double.parseDouble(attributes.getValue(i));
                    } catch (Exception e) {
                    }
ozgun
  • 253
  • 1
  • 2
  • 7
  • This might be of help for you. http://stackoverflow.com/questions/4827344/how-to-parse-xml-using-the-sax-parser/4828765#4828765 – Octavian Helm Apr 18 '11 at 13:00

1 Answers1

1

Annotation based parsers are quite nice at this sort of work and I think that the Simple XML library can handle this for you. You should check it because it may meet your needs in a much better way.

I even wrote a blog post on how to use it in one of your android projects: which you can find here.

Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73