1.xml file:
Use elements to describe data across platforms.
2. Create the XML file in a traditional way, here is a case:
Design ideas: Establish a student management system, create an XML file to save student information;
(1) The first is the layout file Activity_main.xml file, as follows:
<LinearLayoutxmlns: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"android:gravity= "Center_horizontal"android:orientation= "vertical"Android:paddingleft= "10dip"Android:paddingright= "10dip"Tools:context=". Mainactivity " > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Student Information Management system"Android:textcolor= "#99ff0000"android:textsize= "20SP" /> <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Name"Android:textcolor= "#9900ff00"android:textsize= "14SP" /> <EditTextAndroid:id= "@+id/et_name"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter name"Android:inputtype= "text" /> <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "study number"Android:textcolor= "#9900ff00"android:textsize= "14SP" /> <EditTextAndroid:id= "@+id/et_num"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the school number"Android:inputtype= "Number" /> <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Gender"Android:textcolor= "#9900ff00"android:textsize= "14SP" /> <RadiogroupAndroid:id= "@+id/rg_sex"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <RadioButtonAndroid:id= "@+id/rb_male"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:checked= "true"Android:text= "Male" /> <RadioButtonAndroid:id= "@+id/rb_female"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "female" /> </Radiogroup> <RelativelayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <ButtonAndroid:onclick= "Save"Android:layout_alignparentbottom= "true"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Save" /> </Relativelayout></LinearLayout>
:
(2) The logic core code is as follows: Mainactivity.java:
PackageCom.itheima.studentinfo;ImportJava.io.File;ImportJava.io.FileOutputStream;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.widget.EditText;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_name; PrivateEditText Et_num; PrivateRadiogroup Rg_sex; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //No caption for interfacerequestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Et_name=(EditText) Findviewbyid (r.id.et_name); Et_num=(EditText) Findviewbyid (r.id.et_num); Rg_sex=(Radiogroup) Findviewbyid (r.id.rg_sex); } /*** Click Save Student's information to generate an XML file *@paramView*/ Public voidSave (View view) {String name=Et_name.gettext (). toString (). Trim (); String Num=Et_num.gettext (). toString (). Trim (); if(Textutils.isempty (num) | |textutils.isempty (name)) {Toast.maketext ( This, "Student name or school number cannot be empty", 0). Show (); return ; } Try{File File=NewFile (Getfilesdir (), num+ ". xml"); FileOutputStream Fos=Newfileoutputstream (file); StringBuilder SB=NewStringBuilder ();
//Create an XML file in a traditional way Sb.append ( "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"); Sb.append ("<student>"); Sb.append ("<name>"); Sb.append (name); Sb.append ("</name>"); Sb.append ("<num>"); Sb.append (num); Sb.append ("</num>"); Sb.append ("<sex>"); int id = Rg_sex.getcheckedradiobuttonid (); if (Id==r.id.rb_male) {//male Sb.append ("male"); }else{//female Sb.append ("female"); } sb.append ("</sex>"); Sb.append ("</student>");
Fos.write (sb.tostring (). GetBytes ()); Fos.close (); Toast.maketext ( This, "Save data successfully", 0). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "Save data Failed", 0). Show (); } }}
Run the code to the simulator, and then we found the 10001.xml file in the/data/data/com.itheima.studentinfo/files directory, such as:
To export the 10001.xml file to your computer, use your browser to open it as:
The above is a traditional method of building an XML file, but when entering the name, school number and other information, we can not contain illegal characters, such as "/", "<" and so on, so the above code is still flawed. Such as:
Use the browser to open the generated 10002.xml file to report the following error:
It is obvious that the traditional way to create XML files is very limited, but Google's API provides a convenient interface to generate XML files, and do not consider these illegal characters, so it is convenient and secure, the following describes:
Android (Java) Learning Note 185:xml file generation