-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseApplication.java
88 lines (76 loc) · 3.17 KB
/
ParseApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package sachet.example.com.top10downloader;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class ParseApplication{
private static final String TAG = "ParseApplication";
private List<FeedEntry> feed;
public ParseApplication(){
this.feed = new ArrayList<>();
}
public List<FeedEntry> getFeed(){
return feed;
}
public boolean parse(String xmlData){
boolean status = true;
FeedEntry currentReading = null;
boolean inEntry = false;
String textValue = "";
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(xmlData));
int eventType = xpp.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
String tagName = xpp.getName();
switch(eventType){
case XmlPullParser.START_TAG:
// Log.d(TAG, "parse: Starting tag for "+tagName);
if("entry".equalsIgnoreCase(tagName)){
inEntry = true;
currentReading = new FeedEntry();
}
break;
case XmlPullParser.TEXT:
textValue = xpp.getText();
break;
case XmlPullParser.END_TAG:
if(inEntry){
// Log.d(TAG, "parse: Ending tag for "+tagName);
if("entry".equalsIgnoreCase(tagName)){
feed.add(currentReading);
}else if("name".equalsIgnoreCase(tagName)){
currentReading.setName(textValue);
}else if("artist".equalsIgnoreCase(tagName)){
currentReading.setArtist(textValue);
}else if("releaseDate".equalsIgnoreCase(tagName)){
currentReading.setReleaseDate(textValue);
}else if("summary".equalsIgnoreCase(tagName)){
currentReading.setSummary(textValue);
}else if("image".equalsIgnoreCase(tagName)){
currentReading.setImageURL(textValue);
}
}
break;
default:
}
eventType = xpp.next();
}
// for(FeedEntry app:feed){
// Log.d(TAG,"*****************************");
// Log.d(TAG, app.toString());
// }
}catch(Exception e){
// System.out.println("There has been some exception "+ e.getMessage());
e.printStackTrace();
status = false;
}
return status;
}
}