Example of serialized XML file--weather forecast

Source: Internet
Author: User

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivityforecast ">
<button
Android:id= "@+id/btn_xlh"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_alignleft= "@+id/btn_watch"
Android:layout_alignparenttop= "true"
android:layout_margintop= "60DP"
android:text= "serializing XML file"/>
<button
Android:id= "@+id/btn_watch"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/btn_xlh"
Android:layout_centervertical= "true"
Android:text= "How is the weather in Anyang"/>

</RelativeLayout>
<----------------------------------------------------------------------------------------------------------- ------->
<?xml version= "1.0" encoding= "Utf-8"?>
<city>
<name> Anyang </name>
<weather> Sunny </weather>
<temp>27℃</temp>
<info> cool weather suitable for travel </info>
</city>
<----------------------------------------------------------------------------------------------------------- -------->
Package cn.itcast.weatherForecast;

Import Java.io.InputStream;
Import Org.xmlpull.v1.XmlPullParser;

Import Cn.itcast.weatherForecast.bean.WeatherBean;
Import android.app.Activity;
Import Android.content.res.AssetManager;
Import Android.os.Bundle;
Import android.util.Xml;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;

public class Mainactivityforecast extends Activity {
Private Button BTN_XLH;
Private Button Btn_watch;
Private Boolean flag=false;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main_activity_forecast);
Btn_xlh= (Button) Findviewbyid (R.ID.BTN_XLH);
Btn_xlh= (Button) Findviewbyid (R.id.btn_watch);
Btn_xlh.setonclicklistener (New Myonclicklistener ());
Btn_watch.setonclicklistener (New Myonclicklistener ());
}
Private class Myonclicklistener implements onclicklistener{
Private Weatherbean WB = new Weatherbean ();
@Override
public void OnClick (view view) {
Switch (View.getid ()) {
Case R.ID.BTN_XLH:
Flag=true;
Assetmanager am = MainActivityForecast.this.getAssets ();
try {
InputStream is = Am.open ("Weatherforecast.xml");
Xmlpullparser parser = Xml.newpullparser ();
Parser.setinput (IS, "utf-8");
int type = Parser.geteventtype ();
while (type!=xmlpullparser.end_document) {
if (Type==xmlpullparser.start_tag) {
if (Parser.getname (). Equals ("name")) {
Wb.setname (Parser.nexttext ());
}else if (Parser.getname (). Equals ("Weather")) {
Wb.setweather (Parser.nexttext ());
}else if (Parser.getname (). Equals ("temp")) {
Wb.settemp (Parser.nexttext ());
}else if (Parser.getname (). Equals ("info")) {
Wb.setinfo (Parser.nexttext ());
}
}
Type=parser.next ();
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Break
Case R.id.btn_watch:
if (flag) {
Toast.maketext (Mainactivityforecast.this, Wb.tostring (), 1). Show ();
}else{
Toast.maketext (Mainactivityforecast.this, "not serialized, unable to see weather", 1). Show ();
}
Break
}
}
}


@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_activity_forecast, menu);
return true;
}

}

<----------------------------------------------------------------------------------------------------------- -------->
Package Cn.itcast.weatherForecast.bean;

public class Weatherbean {
private String name;
Private String weather;
Private String temp;
Private String info;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetWeather () {
return weather;
}
public void Setweather (String weather) {
This.weather = weather;
}
Public String gettemp () {
return temp;
}
public void Settemp (String temp) {
This.temp = temp;
}
Public String GetInfo () {
return info;
}
public void SetInfo (String info) {
This.info = info;
}
@Override
Public String toString () {
Return "weather conditions: [name=" + name + ", weather=" + Weather + ", temp="
+ temp + ", info=" + info + "]";
}
}

<----------------------------------------------------------------------------------------------------------- -------->
<----------------------------------------------------------------------------------------------------------- -------->

Example of serialized XML file--weather forecast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.