简体   繁体   中英

parsing xml returns only last row

I have this XML

<CurrencyExchangeMap>
<CurrencyExchangePoint>
    <Address>addr 3</Address>
    <Latitude>41.6940265</Latitude>
    <Longitude>44.7985044</Longitude>
</CurrencyExchangePoint>
<CurrencyExchangePoint>
    <Address>addr 4</Address>
    <Latitude>41.7024424</Latitude>
    <Longitude>44.8058617</Longitude>
</CurrencyExchangePoint>
<CurrencyExchangePoint>
    <Address>addr 5</Address>
    <Latitude>41.6954418</Latitude>
    <Longitude>44.7046725</Longitude>
</CurrencyExchangePoint>
</CurrencyExchangeMap>

And I'm parsing it using:

List mapLTList;

private MapLT mapLT;
private String text;

public XMLPullParserHandler() {
    mapLTList = new ArrayList<MapLT>();
}

public List<MapLT> getMapLT(){
    return mapLTList;
}

public List<MapLT> parse(InputStream is){
    XmlPullParserFactory factory = null;
    XmlPullParser parser = null;

    try{
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);

        parser = factory.newPullParser();
        parser.setInput(is, null);

        int eventType = parser.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT){
            String tagname = parser.getName();
            switch (eventType){
                case XmlPullParser.START_TAG:
                    if(tagname.equalsIgnoreCase("CurrencyExchangeMap")){
                        mapLT = new MapLT();
                    }
                    break;
                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;
                case XmlPullParser.END_TAG:
                    if(tagname.equalsIgnoreCase("CurrencyExchangePoint")){
                        mapLTList.add(mapLT);
                    } else if(tagname.equalsIgnoreCase("Address")){
                        mapLT.setAddress(text);
                    } else if(tagname.equalsIgnoreCase("Latitude")){
                        mapLT.setLatitude(Float.parseFloat(text));
                    } else if(tagname.equalsIgnoreCase("Longitude")){
                        mapLT.setLongitude(Float.parseFloat(text));
                    }
                    break;
                default:
                    break;
            }
            eventType = parser.next();
        }

    }catch (Exception ex){
        ex.printStackTrace();
    }
    return  mapLTList;
}

BUT in result there are 3 address and each of them are Addr 5 (last element). What's wrong?

your MapLit contains information about CurrencyExchangePoint (three items in your xml), but you are treating it as CurrencyExchangeMap (one item in your xml). This way you are reusing the same reference over and over, overriding its content. Change

  case XmlPullParser.START_TAG:
      if(tagname.equalsIgnoreCase("CurrencyExchangeMap")){
             mapLT = new MapLT();
      }

with

case XmlPullParser.START_TAG:
  if(tagname.equalsIgnoreCase("CurrencyExchangePoint")){
         mapLT = new MapLT();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM