使用sax解析器解析xml文件后,我得到每个子标记的空值。虽然已解析整个文件,但每个子字段显示的值为null。不会存储值,而是在每个字段中保存null。
XML文件
<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文件
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监听调试结果
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'}