我有一个用于解析XML数据的示例代码,在XML中解析SAX,DOC和XML Pull解析器 哪个是最好的XML解析器,什么是json解析器...?以及如何通过json解析器解析新闻源。请使用任何示例解答
public class MainActivity1 extends Activity {
private String
finalUrl="http://downloads.bbc.co.uk/podcasts/worldservice/globalnews/rss.xml";
private HandleXML obj;
private TextView title,link,description;
private LinearLayout mWeatherInfosLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
finalUrl = intent.getStringExtra("url");
setContentView(R.layout.activity_main_activity1);
mWeatherInfosLayout = (LinearLayout) findViewById(R.id.weather_infos1);
fetch();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void fetch(){
obj = new HandleXML(finalUrl);
obj.fetchXML();
mWeatherInfosLayout.removeAllViews();
while(obj.parsingComplete)
{
if(obj.gotit)
{
final LinearLayout forecastInfoLayout = (LinearLayout)
getLayoutInflater().inflate(R.layout.feedlist, null);
final TextView tvWeather1 = (TextView)
forecastInfoLayout.findViewById(R.id.textview_forecast_info1);
final TextView tvWeather2 = (TextView)
forecastInfoLayout.findViewById(R.id.textview_forecast_info2);
final TextView tvWeather3 = (TextView)
forecastInfoLayout.findViewById(R.id.textview_forecast_info3);
final TextView tvWeather4 = (TextView)
forecastInfoLayout.findViewById(R.id.textview_forecast_info4);
tvWeather1.setText(obj.getTitle());
tvWeather4.setText(obj.getPubDate());
tvWeather2.setText(obj.getLink());
String string = obj.getDescription();
String[] parts = string.split("<");
String part1 = parts[0];
tvWeather3.setText(part1);
mWeatherInfosLayout.addView(forecastInfoLayout);
obj.setgotit();
}
}
}
}
答案 0 :(得分:1)
相应地更改HandleXML类。
public class HandleXML {
private String title = "title";
private String link = "link";
private String description = "description";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = true;
public HandleXML(String url){
this.urlString = url;
}
public String getTitle(){
return title;
}
public String getLink(){
return link;
}
public String getDescription(){
return description;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("title")){
title = text;
}
else if(name.equals("link")){
link = text;
}
else if(name.equals("description")){
description = text;
}
else{
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
}
}
});
thread.start();
}
}