Instantly share code, notes, and snippets.

Embed
What would you like to do?
Main Activity with the XML parsing on the SSaurel's Channel
package com.ssaurel.xmlparsing;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// ...
private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException{
ArrayList<Player> players = new ArrayList<>();
int eventType = parser.getEventType();
Player currentPlayer = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if ("player".equals(eltName)) {
currentPlayer = new Player();
players.add(currentPlayer);
} else if (currentPlayer != null) {
if ("name".equals(eltName)) {
currentPlayer.name = parser.nextText();
} else if ("age".equals(eltName)) {
currentPlayer.age = parser.nextText();
} else if ("position".equals(eltName)) {
currentPlayer.position = parser.nextText();
}
}
break;
}
eventType = parser.next();
}
printPlayers(players);
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment