Created
June 28, 2018 10:01
-
-
Save ssaurel/2870014a65e30ee68895d273fa8c7031 to your computer and use it in GitHub Desktop.
Main Activity with the XML parsing on the SSaurel's Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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