Getting null values after parsing xml file using sax parser in android

南笙酒味 提交于 2019-12-11 02:15:36

问题


After parsing xml file using sax parser I am getting null values for each sub-tag. Although whole file has been parsed but the value showing for each sub field is null. Values are not getting stored and instead null is saved in each field.

XML file

<markers>
    <setMarker id="56c6b9c4ef263869678b4567" mlat="12.947014112057" mlng="77.62656211853"
        slot_id="morning" />
    <setMarker id="56c6b9c5ef263869678b4568" mlat="12.941744189945" mlng="77.628879547119"
        slot_id="morning" />
    <setMarker id="56c6ba01ef263805688b4567" mlat="12.929865544388" mlng="77.633428573608"
        slot_id="morning" />
    <setMarker id="56c6ba11ef2638c3668b4569" mlat="12.93630685197" mlng="77.613000869751"
        slot_id="morning" />
    <setMarker id="56c6ba1aef263814688b4567" mlat="12.944755587651" mlng="77.617979049683"
        slot_id="morning" />
    <setMarker id="56c6ba2bef26383e678b4567" mlat="12.928359760197" mlng="77.609052658081"
        slot_id="morning" />
</marker>

LocationDetails.java

public class LocationDetails {
    public String lattitude;
    public String longitude;
    public String slotId;

    public LocationDetails(String lattitude, String longitude, String slotId) {
        this.lattitude = lattitude;
        this.longitude = longitude;
        this.slotId = slotId;
    }

    public LocationDetails() {

    }

    public String getLattitude() {

        return lattitude;
    }

    public void setLattitude(String lattitude) {
        this.lattitude = lattitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getSlotId() {
        return slotId;
    }

    public void setSlotId(String slotId) {
        this.slotId = slotId;
    }

    @Override
    public String toString() {
        return "LocationDetails{" +
                "lattitude='" + lattitude + '\'' +
                ", longitude='" + longitude + '\'' +
                ", slotId='" + slotId + '\'' +
                '}';
    }

}

SAXXmlHandler.java file

 private List<LocationDetails> locations;
    private String tempVal;
    private LocationDetails tempLocation = null;
    Boolean currentElement = false;

    public SAXXmlHandler() {
        locations = new ArrayList<>();
    }

    public List<LocationDetails> getLocations() {
        return locations;
    }
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        // reset
        currentElement = true;
        tempVal = "";
        if (qName.equalsIgnoreCase("setMarker")) {
            tempLocation = new LocationDetails();
            Log.d("testing", tempLocation.toString());
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (currentElement) {
            Log.d("testone", tempVal);
            tempVal = tempVal +  new String(ch, start, length);
            Log.d("testTwo", tempVal);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;

        if (qName.equalsIgnoreCase("setMarker")) {
            // add it to the list
            locations.add(tempLocation);
        } else if (qName.equalsIgnoreCase("mlat")) {
            tempLocation.setLattitude(tempVal);
        } else if (qName.equalsIgnoreCase("mlng")) {
            tempLocation.setLongitude(tempVal);
        } else if (qName.equalsIgnoreCase("slot_id")) {
            tempLocation.setSlotId(tempVal);
        }
    }
}

MainActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = null;

        list = parse(getResources().openRawResource(R.raw.location_data));

        Log.d("testing", list.toString());

    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    public List<LocationDetails> parse(InputStream inputStream) {
        List<LocationDetails> locations = null;
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            SAXXmlHandler saxHandler = new SAXXmlHandler();
            xr.setContentHandler(saxHandler);
            InputSource inputSource = new InputSource(inputStream);
            xr.parse(inputSource);
            locations = saxHandler.getLocations();

        } catch (Exception e) {
            Log.d("testing", "SAXXMLParser: parse() failed");
            e.printStackTrace();
        }

        return locations;
    }

android monitor debug results

02-29 21:52:13.944 6342-6342/? D/testing: LocationDetails{lattitude='null', longitude='null', slotId='null'}
02-29 21:52:13.944 6342-6342/? D/testing: LocationDetails{lattitude='null', longitude='null', slotId='null'}
02-29 21:52:13.944 6342-6342/? D/testing: LocationDetails{lattitude='null', longitude='null', slotId='null'}
02-29 21:52:13.944 6342-6342/? D/testing: LocationDetails{lattitude='null', longitude='null', slotId='null'}
02-29 21:52:13.944 6342-6342/? D/testing: LocationDetails{lattitude='null', longitude='null', slotId='null'}
02-29 21:52:13.944 6342-6342/? D/testing: LocationDetails{lattitude='null', longitude='null', slotId='null'}

来源:https://stackoverflow.com/questions/35707699/getting-null-values-after-parsing-xml-file-using-sax-parser-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!