Ecco uno article at Developer.com confronto delle prestazioni dei parser DOM, SAX e Pull su Android. Ha trovato che il parser DOM è di gran lunga il più lento, quindi il parser Pull e il parser SAX sono i più veloci nel loro test.
Se hai intenzione di fare un sacco di analisi nella tua applicazione, potrebbe valere la pena di confrontare le diverse opzioni per vedere quale funziona meglio per te.
Ho usato il XmlPullParser
tramite XmlResourceParser
e ho trovato che funzionava bene ed era facile da usare.
Funziona attraverso gli eventi di ritorno XML che ti dicono che cosa si trova lì.
Se lo si utilizza, il codice sarà simile a questa:
XmlResourceParser parser = context.getResources().getXml(R.xml.myfile);
try {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String name = null;
switch (eventType){
case XmlPullParser.START_TAG:
name = parser.getName().toLowerCase();
if (name.equals(SOME_TAG)) {
for (int i = 0;i < parser.getAttributeCount();i++) {
String attribute = parser.getAttributeName(i).toLowerCase();
if (attribute.equals("myattribute")) {
String value = parser.getAttributeValue(i);
}
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
break;
}
eventType = parser.next();
}
}
catch (XmlPullParserException e) {
throw new RuntimeException("Cannot parse XML");
}
catch (IOException e) {
throw new RuntimeException("Cannot parse XML");
}
finally {
parser.close();
}
Se si vuole analizzare XML che non è da un file di risorse è possibile creare un nuovo XmlPullParser
utilizzando il XmlPullParserFactory
class e quindi chiamare il setInput()
method .
fonte
2009-12-18 11:06:20
hi ruibm è utile anche per me .. È possibile mostrare come utilizzare le hashmap anziché le lunghe catene di condizioni if ... – Dinash