I've got this xml:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<alllogs count="5">
<log number="+919876543210" time="1365589432804" date="Wed Apr 10 15:53:52 GMT+05:30 2013" type="2" duration="0" new="1" name="360 chhotein" numbertype="2" numberlabel="null" />
<log number="+919876543210" time="1365595887261" date="Wed Apr 10 17:41:27 GMT+05:30 2013" type="2" duration="0" new="1" name="360 chhotein" numbertype="2" numberlabel="null" />
<log number="+919876543210" time="1365596387590" date="Wed Apr 10 17:49:47 GMT+05:30 2013" type="2" duration="0" new="1" name="360 chhotein" numbertype="2" numberlabel="null" />
<log number="+919876543210" time="1365596787051" date="Wed Apr 10 17:56:27 GMT+05:30 2013" type="2" duration="0" new="1" name="null" numbertype="null" numberlabel="null" />
<log number="0041786095382" time="1365740469738" date="Fri Apr 12 09:51:09 GMT+05:30 2013" type="2" duration="0" new="1" name="null" numbertype="null" numberlabel="null" />
</alllogs>
The parser I'm using is the android native XMLPullParser
I cannot change XML format but I could use another android compatible parser if it's worth it.
If it's not clear it has to fit on an class like this:
try {
String file = Environment.getExternalStorageDirectory() + File.separator + "SmsContactsBackup/logs/calllogs_20130412125502.xml";
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
FileInputStream fis = new FileInputStream(file);
parser.setInput(new InputStreamReader(fis));
Log.d("Response", ""+convertStreamToString(fis));
int eventType = parser.getEventType();
String name = null;
while (eventType != XmlPullParser.END_DOCUMENT)
{
//String name = parser.getName();
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
}else if(eventType == XmlPullParser.START_TAG) {
name = parser.getName();
if (name.equalsIgnoreCase("alllogs")){
count = Integer.parseInt(parser.getAttributeValue("", "count"));
}else if(name.equalsIgnoreCase("log"))
{
phone_no.add(parser.getAttributeValue("", "number"));
}
System.out.println("Start tag "+parser.getName());
}else if(eventType == XmlPullParser.END_TAG) {
name = parser.getName();
System.out.println("End tag "+parser.getName());
}
eventType = parser.next();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
eventtype is not get proper and IOException.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…