通过XmlPullParser填充带有标记文本的Hashmap

时间:2013-09-13 05:39:50

标签: android android-asynctask hashmap xmlpullparser

我有4个值,我试图从xml文件放入一个hashmap。我可以解析xml没问题,但想通过lat / lon获取数据将点放在地图上。我正在尝试从pullparser填充一个hashmap,但是我失败了。我正在讨论我认为在哪里填充hashmap的问题。

这是我的hashmap:

Map<Integer, MapMarkers> DataLocations = new HashMap<Integer, MapMarkers>();

这是我的MapMarkers类:

public class MapMarkers {
    private String lon;
    private String lat;
    private String title;
    private String desc;

    public MapMarkers() {
        super();
    }

    public MapMarkers(String lon, String lat, String title, String desc) {
        super();
        this.lon = lon;
        this.lat = lat;
        this.title = title;
        this.desc = desc;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

}

以下是我的pullparser在异步中运行,因为xml很大:

公共类BuildMapInfoTask扩展了AsyncTask {         String sname = null;         String sdesc = null;         String slat = null;         String slon = null;

    @Override
    protected String doInBackground(String... params) {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory
                    .newInstance();
            factory.setValidating(false);
            XmlPullParser myxml = factory.newPullParser();
            FileInputStream fs = new FileInputStream(
                    "/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");
            myxml.setInput(fs, null);
            int eventType = myxml.getEventType();
            int uniquekey = 0;
            marks = new MapMarkers(slat, slon, sname, sdesc);
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    String tag = myxml.getName();
                    if ("name".equals(tag)) {
                        sname = myxml.nextText().trim();
                        marks.setTitle(sname);
                    } else if ("description".equals(tag)) {
                        sdesc = myxml.nextText().trim();
                        marks.setDesc(sdesc);
                    } else if ("longitude".equals(tag)) {
                        slon = myxml.nextText().trim();
                        marks.setLon(slon);
                    } else if ("latitude".equals(tag)) {
                        slat = myxml.nextText().trim();
                        marks.setLat(slat);
                    }
                    Log.v("ET",uniquekey+"");
                    DataLocations.put(uniquekey, marks);
                    uniquekey++;
                }
                eventType = myxml.next();
            }
        } catch (XmlPullParserException e) {
            Log.e("PP Error", e.getMessage());
        } catch (IOException e) {
            Log.e("PP IOException", e.getMessage());
        }
        return null;

    }

我创建了唯一键作为KEY,然后在值区域中加载lat,lon,title,desc ...我的值未被填充。我在PullParser中需要DataLocations.put(...)来填充DataLocations,如果我当前填充它,我该怎么拉呢?我错了吗?

1 个答案:

答案 0 :(得分:0)

我没有在每个循环中创建一个新的标记对象。这最终为我工作:

 try {
            markerInfo = new HashMap<Marker, MapMarkers>();
            File fXmlFile = new File(
                    "/storage/emulated/0/data/kml/labels.kml");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("Placemark");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                // must make new each time around...
                MapMarkers marks = new MapMarkers();

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    String name = eElement.getElementsByTagName("name").item(0)
                            .getTextContent();

                    String desc = eElement.getElementsByTagName("description")
                            .item(0).getTextContent();

                    String lon = eElement.getElementsByTagName("longitude")
                            .item(0).getTextContent();

                    String lat = eElement.getElementsByTagName("latitude")
                            .item(0).getTextContent();

                    lon = lon.trim();
                    lat = lat.trim();

                    double lati = Double.parseDouble(lat);
                    double lngi = Double.parseDouble(lon);

                    marks.setTitle(name);
                    marks.setDesc(desc);

                    Marker m = map.addMarker(new MarkerOptions()
                            .position(new LatLng(lati, lngi))
                            .title(marks.getTitle())
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.youmarker)));

                    markerInfo.put(m, marks);

                    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
                        @Override
                        public void onInfoWindowClick(Marker marker) {

                            MapMarkers eventInfo = markerInfo.get(marker);

                            msg(Html.fromHtml(eventInfo.getDesc()));

                            // Log.e("ID", marker.getId());
                            // Log.v("EI", eventInfo.getTitle() + " ----- "
                            // + eventInfo.getDesc());

                        }

                    });

                }

            }

        } catch (Exception e) {
            Log.e("Data Buildmap Error", e.getMessage());
        }