Tuesday, June 5, 2012

XML Parsing


private void parseAlertsData(String xmlRecord) throws Exception {
// TODO Auto-generated method stub
DocumentBuilder db = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecord));

Document doc = db.parse(is);
NodeList violation_list = doc.getElementsByTagName("violationlist");

for (int i = 0; i < violation_list.getLength(); i++) {

Element element = (Element) violation_list.item(i);
NodeList geofence_info = doc.getElementsByTagName("geofenceInfo");

for (int j = 0; j < geofence_info.getLength(); j++) {

NodeList geofence_list = element
.getElementsByTagName("geofencelist");

// Toast.makeText(getApplicationContext(),
// "geofenceLength" + geofence_list.getLength(),
// Toast.LENGTH_LONG).show();

for (int k = 0; k < geofence_list.getLength(); k++) {

NodeList vehicle = element
.getElementsByTagName("registratioNo");
NodeList actual_location = element
.getElementsByTagName("actualLocation");
NodeList original_location = element
.getElementsByTagName("originalLocation");
NodeList date = element.getElementsByTagName("date");

Element el_vehicle = (Element) vehicle.item(k);
Element el_actualLocation = (Element) actual_location
.item(k);
Element el_originalLocation = (Element) original_location
.item(k);
Element el_date = (Element) date.item(k);



Str = new String(getCharacterDataFromElement(el_date));
Str2 = new String(
getCharacterDataFromElement(el_originalLocation));
String s[] = Str2.split(",");

String originalLocation = s[0] + "," + s[1];
String date1 = Str.substring(0, 9);
String time1 = Str.substring(10, 16); // Str.length() - 1);

notificationgeofenceData = new NotificationGeoFenceData(getCharacterDataFromElement(el_vehicle),
getCharacterDataFromElement(el_actualLocation),
originalLocation, date1, time1);
geoitem.add(notificationgeofenceData);


}

}

}

}

private String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}

No comments:

Post a Comment