XMLPullParser Out of Memory (Android) - android

I'm stuck in trying to handle an out of memory error in Android while trying to parse a response from a HTTPTransfer using SOAP. Overall the transport is fine until I ask for a large image. The image is about 901KB is size, but for some reason it causes Android to run out of memory while parsing it. Here is the code:
public void parseWithPullParser(InputStream is) {
try {
XmlPullParser parser = GenericHandler.createParser(this.parserTypeName); // new
// org.xmlpull.mxp1.MXParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(is, null);
Log.d(TAG, "Name of class being parsed: " + resultClassName);
for (int eventType = parser.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = parser
.next()) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT: {
break;
}
case XmlPullParser.START_TAG: {
String name = parser.getName();
String prefix = null;
if ("Envelope".equals(name) || "Header".equals(name) || "Body".equals(name)
|| "return".equals(name)) {
prefix = "env:"; // TODO: Hack-Hack-Hack... :)
}
name = prefix == null ? name : prefix + ":" + name;
this.startElement(name);
break;
}
case XmlPullParser.TEXT: {
String text = parser.getText();
if (text != null) {
if (resultClassName.contains("ImageSingle")) {
Log.d(TAG, "Text passage: " + text);
}
if (content == null) {
content = new String();
}
content = text; // Original system used a string builder
// but only for a single section, for
// large images this was a problem, but
// a single string object appears to
// have the same affect
// char[] ch = text.toCharArray(); //original
// this.characters(ch, 0, ch.length); //original
}
break;
}
case XmlPullParser.END_TAG: {
String name = parser.getName();
String prefix = null;
if ("Envelope".equals(name) || "Header".equals(name) || "Body".equals(name)
|| "return".equals(name)) {
prefix = "env:"; // TODO: Hack-Hack-Hack... :)
}
name = prefix == null ? name : prefix + ":" + name;
this.endElement(name);
break;
}
default: {
break;
}
}
}
} catch (Exception except) {
Log.e(this.getClass().getSimpleName(), except.toString(), except);
}
}
I found the library here. The issue (I believe) is when it does parser.next() because it reads in the image data (which is sent to me in a Base64 encoded string) and then tries to do parser.getText(). If I am understanding everything properly the way it outputs the string is by repetitive calls to the internal stringbuilder that will keep repeating .toString() to itself until it generates the parsed string. The image in question is about 1.2 million characters and as each character is 2 bytes, that implies 2.4 MB (the image though is 901 KB originally..but I guess there's extra data that gets parsed?) if I understand this correctly. But the heap expands to over 16 MB which causes the app to crash on stock VM settings when this method is called.
I doubt this is a unique situation and as such would love to hear how others have handled this problem. I've thought about maybe just throwing the string to a file on the SD card to keep it out of memory but it seems that for me to get the string I need parser.getText...which therein lies the problem.

Pursuant to Stephan Branczyk's comment here is my comment extracted and marked as the answer to my question.
For anyone that comes across this, I eventually ended up using a sax parser, I got the idea from here : helloandroid.com/tutorials/newsdroid-rss-reader

Related

Google playstore app version check not working any more

For force update I used to call the app url and check the for the html tag value <softwareVersion><softwareVersion/> but suddenly it stopped working there is no softwareVersion tag in the page so getting null. Is there any google api available to check the google play app version.
Update -
I investigate in more details when I used to call the url the response was 200 but now I am getting 405
I come up with a solution. When ever I am pushing a new version in the playstore I will add the version in the what's new, like this -
WHAT'S NEW
Version - 2.11.0
- New changes 1
- New changes 2
And I look for this Version -
So Full code looks like this -
class VersionCheckTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
String mVer = "";
String mData = "";
try {
URL mUrl = new URL(urls[0]);
BufferedReader in = new BufferedReader(new InputStreamReader(mUrl.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
if (inputLine == null)
break;
mData += inputLine;
}
in.close();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
/*
* we are looking for this tag <div itemprop="description"><content>Version - 2.11.0<br>
* We need to make sure every time we release a new version we should add the line in what's new -
*
* Version - 2.11.1
*
* - New changes 1
* - New changes 2
*/
String startToken = "Version - ";
String endToken = "<";
int index = mData.indexOf(startToken);
if (index == -1) {
mVer = null;
} else {
mVer = mData.substring(index + startToken.length(), index
+ startToken.length() + 100);
mVer = mVer.substring(0, mVer.indexOf(endToken)).trim();
}
return mVer;
}
protected void onPostExecute(String store_version) {
String currentVersion = "";
try {
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
currentVersion = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
Log.v(TAG, "Recv NameNotFoundException. Msg:" + e.getMessage());
}
Log.d(TAG, "store_version: " + store_version);
Log.d(TAG, "device_version: " + currentVersion);
if (store_version != null) {
if (versionCompare(store_version, currentVersion) > 0) {
dialog.setMessage(String.format(getResources().getString(R.string.update_message), getResources().getString(R.string.app_name), store_version));
dialog.show();
} else {
showDisclaimer();
}
}
}
}
public int versionCompare(String storeVersion, String currentVersion) {
String[] vals1 = storeVersion.split("\\.");
String[] vals2 = currentVersion.split("\\.");
int i = 0;
// set index to first non-equal ordinal or length of shortest version string
while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
i++;
}
// compare first non-equal ordinal number
if (i < vals1.length && i < vals2.length) {
int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
return Integer.signum(diff);
}
// the strings are equal or one string is a substring of the other
// e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
return Integer.signum(vals1.length - vals2.length);
}
Maybe, at the end of march, Google changed Play store's HTML Code.
The structure of additional information has also changed.
Some developers, including me, use Jsoup to check for the latest version in the Play Store.
Perhaps you were using code like this:
Document doc = Jsoup.connect
("https://play.google.com/store/apps/details?id=name.package.your").get();
Elements Version = doc.select(".content");
for (Element v : Version) {
if (v.attr("itemprop").equals("softwareVersion")) {
VersionMarket = v.text();
}
}
but, after play store's change, your code return null.
because, "itemprop" and "sofrwareVersion" is gone, like that.
enter image description here
So, you need a new way to parse the version of your app in Google Play store's ADDITION INFORMATION with Jsoup.
try {
Document doc = Jsoup
.connect(
"https://play.google.com/store/apps/details?id=name.package.your")
.get();
Elements Version = doc.select(".htlgb ");
for (int i = 0; i < 5 ; i++) {
VersionMarket = Version.get(i).text();
if (Pattern.matches("^[0-9]{1}.[0-9]{1}.[0-9]{1}$", VersionMarket)) {
break;
}
}
The above code works as follows.
Parsing play store's your app page.
Selecting all "htlgb" contents.
like in image, "3 March 2018", "1,000+" "2.0.4", "4.4 and up, etc."
In [for Loop], [Regex] finds a value matching your version pattern (like 2.0.4) and stops.
VersionMarket is your "app version" and you can use it.
//2018-08-04 Add Comment
For some reason, the code above returns information about "Installs" instead of "version information".
Therefore, if you modify the code as shown below, you can return "version information" again.
try {
Document doc = Jsoup
.connect(
"https://play.google.com/store/apps/details?id=name.package.your")
.get();
Elements Version = doc.select(".htlgb ");
for (int i = 0; i < 10 ; i++) {
VersionMarket = Version.get(i).text();
if (Pattern.matches("^[0-9]{1}.[0-9]{1}.[0-9]{1}$", VersionMarket)) {
break;
}
}
The above code changed the number in "for break" from 5 to 10.
Because the number of "htlgb" codes has changed in Google Play Store's HTML Code.
No, no API exists for checking your app version on Play. Instead, you could implement a solution using Firebase Remote Config, then you have much more control over the minimum version your users see.

how to get inner image url including XML tags Android Studio?

i can't access the inner tag to get the image "url"
here my tag name is "enclosure" and it contain another one called "url" and this is what i want to get...
here the a whole class i created
**public class ParseApplications {
private static final String TAG = "ParseApplications";
private ArrayList<NewsFeeds> application;
public ParseApplications() {
this.application = new ArrayList<>();
}
public ArrayList<NewsFeeds> getApplication() {
return application;
}
public boolean Parse(String xmlData) {
boolean status = true;
NewsFeeds currentNews = null;
boolean InEntry = false;
String textValue = "";
boolean gotImage = false;
try {
// XmlPullParserFactory This class is used to create implementations of XML Pull Parser defined in XMPULL
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//coming line mean that the xml parse i will handle it by my code
/*
Specifies that the parser produced by this factory will provide support for XML namespaces.
By default the value of this is set to false.
Parameters
awareness
boolean: true if the parser produced by this code will provide support for XML namespaces; false otherwise.
*/
factory.setNamespaceAware(true);
//XML Pull Parser is an interface that defines parsing functionality provided in XMLPULL V1 API
//newPullParser is Creates a new instance of a XML Pull Parser using the currently configured factory features.
XmlPullParser xxp = factory.newPullParser();
xxp.setInput(new StringReader(xmlData));
//getEventType Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.). return int
int eventType = xxp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagName = xxp.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
Log.d(TAG, "Parse: Starting tag for " + tagName);
if ("item".equalsIgnoreCase(tagName)) {
InEntry = true;
currentNews = new NewsFeeds();
}
break;
case XmlPullParser.TEXT:
textValue = xxp.getText();
break;
case XmlPullParser.END_TAG:
if (InEntry) {
if ("item".equalsIgnoreCase(tagName)) {
application.add(currentNews);
} else if ("title".equalsIgnoreCase(tagName)) {
currentNews.setName(textValue);
} else if ("pubdate".equalsIgnoreCase(tagName)) {
currentNews.setTheDate(textValue);
} else if ("description".equalsIgnoreCase(tagName)) {
currentNews.setSummry(textValue);
} else if ("link".equalsIgnoreCase(tagName)) {
currentNews.setTitle(textValue);
} else if ("enclosure".equalsIgnoreCase(tagName)) {
currentNews.setImageUrl(textValue);
}
}
break;
default:
//nothing to do
}
eventType = xxp.next();
}
for (NewsFeeds app : application) {
Log.d(TAG, "*********************");
Log.d(TAG, app.toString());
}
} catch (Exception e) {
status = false;
}
return status;
}
}**
i can't access the inner tag to get the image "url" here my tag name is "enclosure" and it contain another one called "url" and this is what i want to get... here the a whole class i created
I suggest printing the raw data and see the hierarchy of the tags. just do a Log without parsing the data and read it yourself. It might be missing or you need to access a parent tag before you get it.
also this might seem basic but are you sure you typed it correctly ? (capital letters, spaces etc)
How about the other tags ? I guess you are getting them correctly .
SOLVED
JUST TWO LINES
else if ("enclosure".equalsIgnoreCase(tagName)) {
String url = xxp.getAttributeValue(0);
currentNews.setImageUrl(url);
}

Android, XMLParser - Reading XML

im currently trying to read and XML from a web server in my Android App, but im not sure how to search for the TAGS, and all the examples i see are not unlike mine:
<document>
<producer/>
<metadata></metadata>
<recorddata count="111">
<row>
<field name="numint" value="MTAwMQ=="/>
<field name="Grupo" value="NQ=="/>
<field name="Link" value="Q29ycmllbnRlcw=="/>
<field name="Nombre" value="Q29ycmllbnRlcw=="/>
<field name="Valor" value="MzQwMC4wMA=="/>
</row>
</recorddata>
</document>
I need to read the ROWS inside RECORDATA, but im not sure how to address this problem, this is my code:
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
ArrayList<Record> Records = null;
int eventType = parser.getEventType();
Record currentRecord = null;
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
Records = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name == "row"){
currentRecord = new Record();
} else if (currentRecord != null){
if (name == "numint"){
currentRecord.numint = parser.getText();
} else if (name == "Grupo"){
currentRecord.group = parser.getText();
} else if (name == "Link"){
currentRecord.link= parser.getText();
}else if (name == "Nombre") {
currentRecord.name= parser.getText();
}else if (name == "Valor") {
currentRecord.value= parser.getText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("row") && currentRecord != null){
Records.add(currentRecord);
}
}
eventType = parser.next();
}
printProducts(Records);
}
BUT, the Records List IS EMPTY at the end of the process, and i can tell is doing something, because there are a lot of records and it takes like 2 minutes till the process is over.
Im not sure if im using the getName() correctly, should i be checking for field??? and if so, how do i know in which field im on. Should i be using nextToken() instead???
I Solved the problem by rewriting my code as follows:
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equals("row")){
currentRecord = new Record();
} else if (currentRecord != null) {
if(parser.getName().equals("field")) {
if(parser.getAttributeValue(null, "name").equalsIgnoreCase("numint")) {
currentRecord.numint = parser.getAttributeValue(null, "value");
}else if (parser.getAttributeValue(null, "name").equalsIgnoreCase("Grupo")) {
currentRecord.group = parser.getAttributeValue(null, "value");
}else if (parser.getAttributeValue(null, "name").equalsIgnoreCase("Link")) {
currentRecord.link = parser.getAttributeValue(null, "value");
}else if (parser.getAttributeValue(null, "name").equalsIgnoreCase("Nombre")) {
currentRecord.name = parser.getAttributeValue(null, "value");
}else if (parser.getAttributeValue(null, "name").equalsIgnoreCase("Valor")) {
currentRecord.value = parser.getAttributeValue(null, "value");
}
}
}
break;
Taking something the answer by Andreaoid, and a little tweaking, i could the read de xml file without issue
To get an attribute you can use the method:
parser.getAttributeValue(null, "value");
Definition:
public abstract String getAttributeValue (String namespace, String name)
Taken from help:
*Added in API level 1. Returns the attributes value identified by namespace URI and namespace localName. If namespaces are disabled namespace must be null. If current event type is not START_TAG then IndexOutOfBoundsException will be thrown.*
Pay attention: Replace all the String comparisons!
name == "row"
With:
name.equals("row")
Hope it helps.

Parsing XML XmlPullParser android

i'm using xmlpullparser in android to parse an xml document that looks like :
<top>
<category>
<name></name>
<desc></desc>
<songs>
<song>
<clip></clip>
<thumb></thumb>
</song>
<song>
<clip></clip>
<thumb></thumb>
</song>
</songs>
</category>
</top>
I tried this :
while (eventType != XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
categoriesSong = new ArrayList<TopMousika>();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase(CATEGORY)){
currentCategory = new TopMousika();
currentCategory.setId(parser.getAttributeValue(0));
currentCategory.setId(parser.getAttributeValue(1));
} else if (currentCategory != null){
if (name.equalsIgnoreCase(NAME)){
currentCategory.setName(parser.nextText());
} else if (name.equalsIgnoreCase(DESCRIPTION)){
currentCategory.setDescription(parser.nextText());
} else if (name.equalsIgnoreCase(THUMBNAIL)){
currentCategory.setThumbnail(parser.nextText());
} else if (name.equalsIgnoreCase(SONGS)){
songs = new ArrayList<SongMousika>();
if(name.equalsIgnoreCase(SONG)){
currentSong = new SongMousika();
currentSong.setId(parser.getAttributeValue(0));
Log.d("TEST", "OK");
songs.add(currentSong);
} else if (name.equalsIgnoreCase(TITLE)){
Log.d("TEST", "OK2");
currentSong.setTitle(parser.nextText());
} else if (name.equalsIgnoreCase(SINGER)){
currentSong.setTitle(parser.nextText());
} else if (name.equalsIgnoreCase(THUMBNAIL)){
currentSong.setTitle(parser.nextText());
} else if (name.equalsIgnoreCase(PUBLICATION_DATE)){
currentSong.setTitle(parser.nextText());
} else if (name.equalsIgnoreCase(CLIP)){
currentSong.setTitle(parser.nextText());
}
currentCategory.setSongs(songs);
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase(CATEGORY) &&
currentCategory != null){
currentCategory.setSongs(songs);
categoriesSong.add(currentCategory);
} else if (name.equalsIgnoreCase(TOP)){
done = true;
}
break;
}
eventType = parser.next();
}
but I can not retrieve my Songs List.
can any one help me please ?
You seem to be expecting name to change magically between checks:
if (name.equalsIgnoreCase(SONGS)) {
songs = new ArrayList<SongMousika>();
if(name.equalsIgnoreCase(SONG)) {
How is it going to be SONG and SONGS? You need to keep pulling XML and react to each element name differently in the loop. So you'll probably have a bunch of if/else if statements without any nesting when reacting to a START_TAG event. (It's very likely to be worth pulling the handling of that into a separate method, by the way.)
EDIT: Okay, so you need to make each iteration of the loop just react to one tag. So your handling for a start tag would be something like:
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase(CATEGORY)){
currentCategory = new TopMousika();
currentCategory.setId(parser.getAttributeValue(0));
currentCategory.setId(parser.getAttributeValue(1));
} else if (currentCategory != null) {
if (name.equalsIgnoreCase(NAME)){
currentCategory.setName(parser.nextText());
} else if (name.equalsIgnoreCase(DESCRIPTION)){
currentCategory.setDescription(parser.nextText());
} else if (name.equalsIgnoreCase(THUMBNAIL)){
currentCategory.setThumbnail(parser.nextText());
} else if (name.equalsIgnoreCase(SONGS)){
songs = new ArrayList<SongMousika>();
} else if (songs != null) {
if(name.equalsIgnoreCase(SONG)) {
currentSong = new SongMousika();
currentSong.setId(parser.getAttributeValue(0));
Log.d("TEST", "OK");
songs.add(currentSong);
} else if (currentSong != null) {
else if (name.equalsIgnoreCase(TITLE)) {
Log.d("TEST", "OK2");
currentSong.setTitle(parser.nextText());
} else if (name.equalsIgnoreCase(SINGER)){
currentSong.setSinger(parser.nextText());
} else if (name.equalsIgnoreCase(THUMBNAIL))
// etc
}
}
}
Note how in any path through there we never check for name having multiple values - we say:
Are we starting a new category?
If so, create it and remember it - then continue with the next iteration.
If not (and if we've got a category), are we starting a new song list?
If so, create it and remember it - then continue with the next iteration.
If not (and if we've got a song list), are we starting a new song?
If so, create it and remember it - then continue with the next iteration.
If not (and if we've got a song)...
Are we reading the title? If so, read the text and set it, then continue.
Are we reading the singer? If so, read the text and set it, then continue.
Are we reading the thumbnail? If so, read the text and set it, then continue.
etc

Store static data in Android - custom resource?

I'm new to Android development, and I've been playing around with it a bit. I was trying to create a program that has a small database-like collection of never-changing data. In C#, my currently best language, I'd use a List of a custom class and serialize that to an xml file, then read that into my application at runtime. I found the /xml resource folder in Android, but I'm not sure how I would go about doing what I'm envisioning. What would be the best way to go about doing this?
The data will never need to change. Example:
Blob | A | B
----------------
Blob 1 | 23 | 42
Blob 2 | 34 | 21
I know that's laid out like a table, but using a database doesn't really make sense to me because the data will never change, and I would need a way to store it to initially populate the database anyway.
So basically I'm looking for a way to store somewhat-complex static data in my application. Any ideas?
EDIT: I also saw the /raw folder. So I could store things in /res/raw or /res/xml. But I'm not sure what would be the best way to store/parse the data...
I think this is the BEST solution and i am already using this one to store Static-data in my every project.
For that...
You can do one thing, make one xml file namely "temp.xml" ..and store the data in temp.xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<rootelement1>
<subelement> Blob 1
<subsubelement> 23 </subsubelement>
<subsubelement> 42 </subsubelement>
</subelement>
<subelement>Blob 2
<subsubelement> 34 </subsubelement>
<subsubelement> 21 </subsubelement>
</subelement>
</rootelement1>
and then use XML PullParser technique to parse data.
You can have coding samples of PullParsing technique on Example , refer this example for better idea.
Enjoy!!
The best way is to use the Android Resource Heirarchy.
In the res/values/ directory, you can store any number of key-value pairs for several basic data types. In your app, you would refer to them using an autogenerated resource id (name based on your resource's key). See the link above for more documentation and details.
Android also supports raw datafiles. You could store your data in the file directory under res/raw/yourfile.dat
You you create your data in whatever text based format you want and then read it on activity startup using the resource access apis.
I have used Simple for xml parsing in the past. I think it has the least amount of code if you know what to expect in xml, which in your case you do.
http://simple.sourceforge.net/
According to the doc, /xml is the way to go.
Providing Resources
xml/ Arbitrary XML files that can be read at run-time by calling
Resources.getXML().
Various XML configuration files must be saved here, such as a searchable configuration.
Documentation for getXML()
I also made a working example:
the XML structure:
<?xml version="1.0" encoding="utf-8"?>
<quizquestions>
<quizquestion>
<header_image_src>ic_help_black_24dp</header_image_src>
<question>What is the Capital of U.S.A.?</question>
<input_type>Radio</input_type>
<answer correct="false">New York City</answer>
<answer correct="true">Washington D.C.</answer>
<answer correct="false">Chicago</answer>
<answer correct="false">Philadelphia</answer>
</quizquestion>
<quizquestion>
<header_image_src>ic_help_black_24dp</header_image_src>
<question>What is the family name of the famous dutch painter Vincent Willem van .... ?</question>
<input_type>EditText</input_type>
<answer correct="true">Gogh</answer>
</quizquestion>
</quizquestions>
the Java class to hold parsed data:
public class QuizQuestion {
private int headerImageResId;
private String question;
private String inputType;
private ArrayList<String> answers;
private ArrayList<Boolean> answerIsCorrect;
private ArrayList<Integer> correctAnswerIndexes;
/**
* constructor for QuizQuestion object
*/
QuizQuestion() {
headerImageResId = 0;
question = null;
inputType = null;
answers = new ArrayList<>();
answerIsCorrect = new ArrayList<>();
correctAnswerIndexes = new ArrayList<>();
}
public void setHeaderImageResId(int headerImageResId) {
this.headerImageResId = headerImageResId;
}
public int getHeaderImageResId() {
return headerImageResId;
}
void setQuestion(String question) {
this.question = question;
}
public String getQuestion() {
return question;
}
void setInputType(String inputType) {
this.inputType = inputType;
}
public String getInputType() {
return inputType;
}
void addAnswer(String answer, boolean isCorrect)
{
if (isCorrect)
correctAnswerIndexes.add(answers.size());
answers.add(answer);
answerIsCorrect.add(isCorrect);
}
public ArrayList<String> getAnswers() {
return answers;
}
public String getAnswer(int index)
{
// check index to avoid out of bounds exception
if (index < answers.size()) {
return answers.get(index);
}
else
{
return null;
}
}
public int size()
{
return answers.size();
}
}
the parser itself:
/**
* Created by bivanbi on 2017.02.23..
*
* class to parse xml resource containing quiz data into ArrayList of QuizQuestion objects
*
*/
public class QuizXmlParser {
public static String lastErrorMessage = "";
/**
* static method to parse XML data into ArrayList of QuizQuestion objects
* #param activity is the calling activity
* #param xmlResourceId is the resource id of XML resource to be parsed
* #return null if parse error is occurred or ArrayList of objects if successful
* #throws XmlPullParserException
* #throws IOException
*/
public static ArrayList<QuizQuestion> parse(Activity activity, int xmlResourceId)
throws XmlPullParserException, IOException
{
String logTag = QuizXmlParser.class.getSimpleName();
Resources res = activity.getResources();
XmlResourceParser quizDataXmlParser = res.getXml(R.xml.quiz_data);
ArrayList<String> xmlTagStack = new ArrayList<>();
ArrayList<QuizQuestion> quizQuestions = new ArrayList<>();
QuizQuestion currentQuestion = null;
boolean isCurrentAnswerCorrect = false;
quizDataXmlParser.next();
int eventType = quizDataXmlParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
// begin document
if(eventType == XmlPullParser.START_DOCUMENT)
{
Log.d(logTag,"Begin Document");
}
// begin tag
else if(eventType == XmlPullParser.START_TAG)
{
String tagName = quizDataXmlParser.getName();
xmlTagStack.add(tagName);
Log.d(logTag,"Begin Tag "+tagName+", depth: "+xmlTagStack.size());
Log.d(logTag,"Tag "+tagName+" has "+quizDataXmlParser.getAttributeCount()+" attribute(s)");
// this is a beginning of a quiz question tag so create a new QuizQuestion object
if (tagName.equals("quizquestion")){
currentQuestion = new QuizQuestion();
}
else if(tagName.equals("answer"))
{
isCurrentAnswerCorrect = quizDataXmlParser.getAttributeBooleanValue(null,"correct",false);
if (isCurrentAnswerCorrect == true) {
Log.d(logTag, "Tag " + tagName + " has attribute correct = true");
}
else
{
Log.d(logTag, "Tag " + tagName + " has attribute correct = false");
}
}
}
// end tag
else if(eventType == XmlPullParser.END_TAG)
{
String tagName = quizDataXmlParser.getName();
if (xmlTagStack.size() < 1)
{
lastErrorMessage = "Error 101: encountered END_TAG "+quizDataXmlParser.getName()+" while TagStack is empty";
Log.e(logTag, lastErrorMessage);
return null;
}
xmlTagStack.remove(xmlTagStack.size()-1);
Log.d(logTag,"End Tag "+quizDataXmlParser.getName()+", depth: "+xmlTagStack.size());
// reached the end of a quizquestion definition, add it to the array
if (tagName.equals("quizquestion")){
if (currentQuestion != null)
quizQuestions.add(currentQuestion);
currentQuestion = null;
}
}
// text between tag begin and end
else if(eventType == XmlPullParser.TEXT)
{
String currentTag = xmlTagStack.get(xmlTagStack.size()-1);
String text = quizDataXmlParser.getText();
Log.d(logTag,"Text: "+text+", current tag: "+currentTag+", depth: "+xmlTagStack.size());
if (currentQuestion == null) {
Log.e(logTag,"currentQuestion is not initialized! text: "+text+", current tag: "+currentTag+", depth: "+xmlTagStack.size());
continue;
}
if (currentTag.equals("header_image_src"))
{
int drawableResourceId = activity.getResources().getIdentifier(text, "drawable", activity.getPackageName());
currentQuestion.setHeaderImageResId(drawableResourceId);
}
else if (currentTag.equals("question"))
{
currentQuestion.setQuestion(text);
}
else if (currentTag.equals("answer"))
{
currentQuestion.addAnswer(text, isCurrentAnswerCorrect);
}
else if (currentTag.equals("input_type"))
{
currentQuestion.setInputType(text);
}
else
{
Log.e(logTag,"Unexpected tag "+currentTag+" with text: "+text+", depth: "+xmlTagStack.size());
}
}
eventType = quizDataXmlParser.next();
}
Log.d(logTag,"End Document");
return quizQuestions;
}
}
and finally, calling the parser:
// read quiz data from xml resource quiz_data
try {
quizQuestions = QuizXmlParser.parse(this,R.xml.quiz_data);
Log.d("Main","QuizQuestions: "+quizQuestions);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
quizQuestions = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
quizQuestions = null;
}
if (quizQuestions == null)
{
Toast.makeText(this,"1001 Failed to parse Quiz XML, sorry", Toast.LENGTH_LONG).show();
finish();
}

Categories

Resources