ozgun
ozgun

Reputation: 263

Problem with extracting data from xml

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) {
                    }

Upvotes: 0

Views: 377

Answers (1)

Robert Massaioli
Robert Massaioli

Reputation: 13477

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.

Upvotes: 1

Related Questions