public class MainActivity extends Activity {
TextView myText = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("11111111111111111");
myText = (TextView)findViewById(R.id.myText);
try {
XmlPullParserFactory xmlFac = XmlPullParserFactory.newInstance();
XmlPullParser xmlParser = xmlFac.newPullParser();
InputStream in= MainActivity.this.getResources().openRawResource(R.xml.pull);
xmlParser.setInput(in, "utf-8");
System.out.println("222222222222222222");
StringBuilder sb = new StringBuilder(" ");
int event = xmlParser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
System.out.println("333333333333333333333");
// 为何if语句无法执行。
if(event == XmlPullParser.START_TAG){
System.out.println("444444444444444444444");
String tagName = xmlParser.getName();
if(tagName.equals("book")){
String text = xmlParser.nextText();
sb.append(text);
}
}
System.out.println("555555555555555555");
event = xmlParser.next();
}
myText.setText(sb.toString());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}