編輯:關於Android編程
有些時候,我們需要生成一個XML文件,生成XML文件的方法有很多,
如:
可以只使用一個StringBuilder組拼XML內容,然後把內容寫入到文件中;
或者使用DOM API生成XML文件,或者也可以使用pull解析器生成XML文件,這裡推薦大家使用Pull解析器。
Strings.xml
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">lession03_xml</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="btn_sax">采用sax解析xml文件</string>
<string name="btn_dom">采用dom解析方式解析</string>
<string name="btn_pull">采用pull解析方式解析</string>
<string name="btn_cpull">采用pull解析生成xml文件</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">lession03_xml</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="btn_sax">采用sax解析xml文件</string>
<string name="btn_dom">采用dom解析方式解析</string>
<string name="btn_pull">采用pull解析方式解析</string>
<string name="btn_cpull">采用pull解析生成xml文件</string>
</resources>
activity_xml.xml
[html] view plaincopyprint?
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".XmlActivity" >
<Button
android:id="@+id/button_cpull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_pull"
android:layout_alignRight="@+id/btn_pull"
android:layout_below="@+id/btn_pull"
android:layout_marginTop="20dp"
android:text="@string/btn_cpull" />
<Button
android:id="@+id/btn_sax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/btn_sax" />
<Button
android:id="@+id/btn_pull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_dom"
android:layout_alignRight="@+id/btn_dom"
android:layout_below="@+id/btn_dom"
android:layout_marginTop="68dp"
android:text="@string/btn_pull" />
<Button
android:id="@+id/btn_dom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_sax"
android:layout_alignRight="@+id/btn_sax"
android:layout_below="@+id/btn_sax"
android:layout_marginTop="60dp"
android:text="@string/btn_dom" />
</RelativeLayout>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".XmlActivity" >
<Button
android:id="@+id/button_cpull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_pull"
android:layout_alignRight="@+id/btn_pull"
android:layout_below="@+id/btn_pull"
android:layout_marginTop="20dp"
android:text="@string/btn_cpull" />
<Button
android:id="@+id/btn_sax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/btn_sax" />
<Button
android:id="@+id/btn_pull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_dom"
android:layout_alignRight="@+id/btn_dom"
android:layout_below="@+id/btn_dom"
android:layout_marginTop="68dp"
android:text="@string/btn_pull" />
<Button
android:id="@+id/btn_dom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_sax"
android:layout_alignRight="@+id/btn_sax"
android:layout_below="@+id/btn_sax"
android:layout_marginTop="60dp"
android:text="@string/btn_dom" />
</RelativeLayout>
Person.java
[java] view plaincopyprint?
package com.example.lession03_xml.domain;
import java.io.Serializable;
/**
* 創建一個javabean存儲xml解析的數據
*
* @author chenhj
*
*/
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private short age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String name, short age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getAge() {
return age;
}
public void setAge(short age) {
this.age = age;
}
}
package com.example.lession03_xml.domain;
import java.io.Serializable;
/**
* 創建一個javabean存儲xml解析的數據
*
* @author chenhj
*
*/
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private short age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String name, short age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getAge() {
return age;
}
public void setAge(short age) {
this.age = age;
}
}
XMLPullService.java
[java] view plaincopyprint?
package com.example.lession03_xml.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.R.bool;
import android.os.Environment;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.Xml;
import com.example.lession03_xml.domain.Person;
/**
* 采用pull解析xml文件 事件驅動 開始,結束元素事件 產生的事件是一個數字,通過switch語句體現
*
* @author zhaoyazhi
*
*/
public class XMLPullService {
<SPAN style="WHITE-SPACE: pre"> </SPAN>public List<Person> getPerson(InputStream inputStream) throws Exception {
<SPAN style="WHITE-SPACE: pre"> </SPAN>List<Person> persons = null;
<SPAN style="WHITE-SPACE: pre"> </SPAN>Person person = null;
<SPAN style="WHITE-SPACE: pre"> </SPAN>// android提供一個類XmlPullParser,得到pull解析器
<SPAN style="WHITE-SPACE: pre"> </SPAN>XmlPullParser pullParser = Xml.newPullParser();
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 設置要進行解析的內容,不是立刻發生,調用才發生
<SPAN style="WHITE-SPACE: pre"> </SPAN>pullParser.setInput(inputStream, "utf-8");
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* <?xml version="1.0" encoding="UTF-8"?> */
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 觸發第一個事件 用數字形式表示 開始文檔事件
<SPAN style="WHITE-SPACE: pre"> </SPAN>int event = pullParser.getEventType();
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 不等於文檔的結束時間
<SPAN style="WHITE-SPACE: pre"> </SPAN>while (event != XmlPullParser.END_DOCUMENT) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 屬於開始文檔事件
<SPAN style="WHITE-SPACE: pre"> </SPAN>switch (event) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>case XmlPullParser.START_DOCUMENT:
<SPAN style="WHITE-SPACE: pre"> </SPAN>persons = new ArrayList<Person>();
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>case XmlPullParser.START_TAG:
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* <persons> */
<SPAN style="WHITE-SPACE: pre"> </SPAN>if ("person".equals(pullParser.getName())) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* <person id="23"> */
<SPAN style="WHITE-SPACE: pre"> </SPAN>int id = new Integer(pullParser.getAttributeValue(0));
<SPAN style="WHITE-SPACE: pre"> </SPAN>person = new Person();
<SPAN style="WHITE-SPACE: pre"> </SPAN>person.setId(id);
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>if (person != null) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* <name> */
<SPAN style="WHITE-SPACE: pre"> </SPAN>if ("name".equals(pullParser.getName())) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 返回文本的值
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* 李明 */
<SPAN style="WHITE-SPACE: pre"> </SPAN>person.setName(pullParser.nextText());
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* <age> */
<SPAN style="WHITE-SPACE: pre"> </SPAN>if ("age".equals(pullParser.getName())) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* 30 */person.setAge(new Short(pullParser.nextText()));
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 結束元素事件
<SPAN style="WHITE-SPACE: pre"> </SPAN>/* </persons> */
<SPAN style="WHITE-SPACE: pre"> </SPAN>case XmlPullParser.END_TAG:
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 當前解析器指向的節點為person
<SPAN style="WHITE-SPACE: pre"> </SPAN>if ("person".equals(pullParser.getName())) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>persons.add(person);
<SPAN style="WHITE-SPACE: pre"> </SPAN>person = null;
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 解析器向後解析
<SPAN style="WHITE-SPACE: pre"> </SPAN>event = pullParser.next();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>return persons;
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 寫入
<SPAN style="WHITE-SPACE: pre"> </SPAN>public boolean save(List<Person> persons) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 采用pull解析器進行實現
<SPAN style="WHITE-SPACE: pre"> </SPAN>if (Environment.getExternalStorageState().equals(
<SPAN style="WHITE-SPACE: pre"> </SPAN>Environment.MEDIA_MOUNTED)) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 獲取SD卡目錄 文件對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>File sdCardDir = Environment.getExternalStorageDirectory();
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 創建文件
<SPAN style="WHITE-SPACE: pre"> </SPAN>File file = new File(sdCardDir, "mycsdn.xml");
<SPAN style="WHITE-SPACE: pre"> </SPAN>// xml持久化器
<SPAN style="WHITE-SPACE: pre"> </SPAN>XmlSerializer serializer = Xml.newSerializer();
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>FileOutputStream fos = null;
<SPAN style="WHITE-SPACE: pre"> </SPAN>try {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 根據文件對象創建一個文件的輸出流對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>fos = new FileOutputStream(file);
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 設置輸出流及編碼
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.setOutput(fos, "UTF-8");
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 設置文件的開始
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.startDocument("utf-8", true);
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>// persons標簽開始
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.startTag(null, "persons");
<SPAN style="WHITE-SPACE: pre"> </SPAN>for (Person person : persons) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// person標簽開始
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.startTag(null, "person");
<SPAN style="WHITE-SPACE: pre"> </SPAN>// person 屬性
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.attribute(null, "id", person.getId() + "");
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 設置person標簽的子標簽name
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.startTag(null, "name");
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.text(person.getName() + "");
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.endTag(null, "name");
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 設置person標簽子標簽age
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.startTag(null, "age");
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.text(person.getAge() + "");
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.endTag(null, "age");
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>// person標簽結束
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.endTag(null, "person");
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>// persons標簽的結束
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.endTag(null, "persons");
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 文件的結束
<SPAN style="WHITE-SPACE: pre"> </SPAN>serializer.endDocument();
<SPAN style="WHITE-SPACE: pre"> </SPAN>
<SPAN style="WHITE-SPACE: pre"> </SPAN>fos.flush();
<SPAN style="WHITE-SPACE: pre"> </SPAN>fos.close();
<SPAN style="WHITE-SPACE: pre"> </SPAN>return true;
<SPAN style="WHITE-SPACE: pre"> </SPAN>} catch (Exception e) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>e.printStackTrace();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>return false;
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
}
package com.example.lession03_xml.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.R.bool;
import android.os.Environment;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.Xml;
import com.example.lession03_xml.domain.Person;
/**
* 采用pull解析xml文件 事件驅動 開始,結束元素事件 產生的事件是一個數字,通過switch語句體現
*
* @author zhaoyazhi
*
*/
public class XMLPullService {
public List<Person> getPerson(InputStream inputStream) throws Exception {
List<Person> persons = null;
Person person = null;
// android提供一個類XmlPullParser,得到pull解析器
XmlPullParser pullParser = Xml.newPullParser();
// 設置要進行解析的內容,不是立刻發生,調用才發生
pullParser.setInput(inputStream, "utf-8");
/* <?xml version="1.0" encoding="UTF-8"?> */
// 觸發第一個事件 用數字形式表示 開始文檔事件
int event = pullParser.getEventType();
// 不等於文檔的結束時間
while (event != XmlPullParser.END_DOCUMENT) {
// 屬於開始文檔事件
switch (event) {
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
/* <persons> */
if ("person".equals(pullParser.getName())) {
/* <person id="23"> */
int id = new Integer(pullParser.getAttributeValue(0));
person = new Person();
person.setId(id);
}
if (person != null) {
/* <name> */
if ("name".equals(pullParser.getName())) {
// 返回文本的值
/* 李明 */
person.setName(pullParser.nextText());
}
/* <age> */
if ("age".equals(pullParser.getName())) {
/* 30 */person.setAge(new Short(pullParser.nextText()));
}
}
break;
// 結束元素事件
/* </persons> */
case XmlPullParser.END_TAG:
// 當前解析器指向的節點為person
if ("person".equals(pullParser.getName())) {
persons.add(person);
person = null;
}
break;
}
// 解析器向後解析
event = pullParser.next();
}
return persons;
}
// 寫入
public boolean save(List<Person> persons) {
// 采用pull解析器進行實現
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 獲取SD卡目錄 文件對象
File sdCardDir = Environment.getExternalStorageDirectory();
// 創建文件
File file = new File(sdCardDir, "mycsdn.xml");
// xml持久化器
XmlSerializer serializer = Xml.newSerializer();
FileOutputStream fos = null;
try {
// 根據文件對象創建一個文件的輸出流對象
fos = new FileOutputStream(file);
// 設置輸出流及編碼
serializer.setOutput(fos, "UTF-8");
// 設置文件的開始
serializer.startDocument("utf-8", true);
// persons標簽開始
serializer.startTag(null, "persons");
for (Person person : persons) {
// person標簽開始
serializer.startTag(null, "person");
// person 屬性
serializer.attribute(null, "id", person.getId() + "");
// 設置person標簽的子標簽name
serializer.startTag(null, "name");
serializer.text(person.getName() + "");
serializer.endTag(null, "name");
// 設置person標簽子標簽age
serializer.startTag(null, "age");
serializer.text(person.getAge() + "");
serializer.endTag(null, "age");
// person標簽結束
serializer.endTag(null, "person");
}
// persons標簽的結束
serializer.endTag(null, "persons");
// 文件的結束
serializer.endDocument();
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}
XmlActivity.java
[java] view plaincopyprint?
package com.example.lession03_xml;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.example.lession03_xml.domain.Person;
import com.example.lession03_xml.service.XMLContentHandlerService;
import com.example.lession03_xml.service.XMLDomService;
import com.example.lession03_xml.service.XMLPullService;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class XmlActivity extends Activity {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 聲明控件
<SPAN style="WHITE-SPACE: pre"> </SPAN>public Button btn_sax, btn_dom, btn_pull, btn_cpull;
<SPAN style="WHITE-SPACE: pre"> </SPAN>public XMLDomService xmlDomService;
<SPAN style="WHITE-SPACE: pre"> </SPAN>public XMLPullService xmlPullService;
<SPAN style="WHITE-SPACE: pre"> </SPAN>@Override
<SPAN style="WHITE-SPACE: pre"> </SPAN>protected void onCreate(Bundle savedInstanceState) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>super.onCreate(savedInstanceState);
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 設置顯示的視圖
<SPAN style="WHITE-SPACE: pre"> </SPAN>setContentView(R.layout.activity_xml);
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 實例化
<SPAN style="WHITE-SPACE: pre"> </SPAN>xmlDomService = new XMLDomService();
<SPAN style="WHITE-SPACE: pre"> </SPAN>xmlPullService = new XMLPullService();
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 通過findViewById方法獲取控件對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_sax = (Button) findViewById(R.id.btn_sax);
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_dom = (Button) findViewById(R.id.btn_dom);
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_pull = (Button) findViewById(R.id.btn_pull);
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_cpull = (Button) findViewById(R.id.button_cpull);
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 給按鈕注冊事件
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_sax.setOnClickListener(new MyClickListener());
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_dom.setOnClickListener(new MyClickListener());
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_pull.setOnClickListener(new MyClickListener());
<SPAN style="WHITE-SPACE: pre"> </SPAN>btn_cpull.setOnClickListener(new MyClickListener());
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>@Override
<SPAN style="WHITE-SPACE: pre"> </SPAN>public boolean onCreateOptionsMenu(Menu menu) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// Inflate the menu; this adds items to the action bar if it is present.
<SPAN style="WHITE-SPACE: pre"> </SPAN>getMenuInflater().inflate(R.menu.xml, menu);
<SPAN style="WHITE-SPACE: pre"> </SPAN>return true;
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>class MyClickListener implements View.OnClickListener {
<SPAN style="WHITE-SPACE: pre"> </SPAN>@Override
<SPAN style="WHITE-SPACE: pre"> </SPAN>public void onClick(View v) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 獲取組件的id
<SPAN style="WHITE-SPACE: pre"> </SPAN>int id = v.getId();
<SPAN style="WHITE-SPACE: pre"> </SPAN>switch (id) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>case R.id.btn_sax:
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.makeText(XmlActivity.this, "采用sax解析xml文件案例",
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.LENGTH_LONG).show();
<SPAN style="WHITE-SPACE: pre"> </SPAN>// sax解析的工廠對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>SAXParserFactory factory = SAXParserFactory.newInstance();
<SPAN style="WHITE-SPACE: pre"> </SPAN>try {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 得到sax的解析器
<SPAN style="WHITE-SPACE: pre"> </SPAN>SAXParser saxParser = factory.newSAXParser();
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 創建handler對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>XMLContentHandlerService handlerService = new XMLContentHandlerService();
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 獲取到了 asserts目錄中 csdn.xml文件
<SPAN style="WHITE-SPACE: pre"> </SPAN>InputStream is = getAssets().open("csdn.xml");
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 直接解析 InputStream的流對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>saxParser.parse(is, handlerService);
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 通過 handlerService對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.makeText(XmlActivity.this,
<SPAN style="WHITE-SPACE: pre"> </SPAN>"---" + handlerService.getPersons().size(),
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.LENGTH_LONG).show();
<SPAN style="WHITE-SPACE: pre"> </SPAN>} catch (Exception e) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// TODO Auto-generated catch block
<SPAN style="WHITE-SPACE: pre"> </SPAN>e.printStackTrace();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>case R.id.btn_dom:
<SPAN style="WHITE-SPACE: pre"> </SPAN>InputStream is = null;
<SPAN style="WHITE-SPACE: pre"> </SPAN>try {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 獲取讀取文件的輸入流對象
<SPAN style="WHITE-SPACE: pre"> </SPAN>is = getAssets().open("csdn.xml");
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 采用
<SPAN style="WHITE-SPACE: pre"> </SPAN>List<Person> persons = xmlDomService.parseXML(is);
<SPAN style="WHITE-SPACE: pre"> </SPAN>//
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.makeText(XmlActivity.this,
<SPAN style="WHITE-SPACE: pre"> </SPAN>"" + persons.get(0).getName(), Toast.LENGTH_LONG)
<SPAN style="WHITE-SPACE: pre"> </SPAN>.show();
<SPAN style="WHITE-SPACE: pre"> </SPAN>} catch (Exception e) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>e.printStackTrace();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>case R.id.btn_pull:
<SPAN style="WHITE-SPACE: pre"> </SPAN>List<Person> persons = null;
<SPAN style="WHITE-SPACE: pre"> </SPAN>try {
<SPAN style="WHITE-SPACE: pre"> </SPAN>persons = xmlPullService.getPerson(getAssets().open(
<SPAN style="WHITE-SPACE: pre"> </SPAN>"csdn.xml"));
<SPAN style="WHITE-SPACE: pre"> </SPAN>} catch (Exception e) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>e.printStackTrace();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>if (persons != null) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.makeText(
<SPAN style="WHITE-SPACE: pre"> </SPAN>XmlActivity.this,
<SPAN style="WHITE-SPACE: pre"> </SPAN>"采用pull解析xml文件" + persons.get(0).getName() + "--"
<SPAN style="WHITE-SPACE: pre"> </SPAN>+ persons.get(1).getName(),
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.LENGTH_LONG).show();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>case R.id.button_cpull:
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 重新生成一個xml文件 mycsdn.xml
<SPAN style="WHITE-SPACE: pre"> </SPAN>List<Person> persons1 = null;
<SPAN style="WHITE-SPACE: pre"> </SPAN>try {
<SPAN style="WHITE-SPACE: pre"> </SPAN>persons1 = xmlPullService.getPerson(getAssets().open(
<SPAN style="WHITE-SPACE: pre"> </SPAN>"csdn.xml"));
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 重新生成一個新xml文件
<SPAN style="WHITE-SPACE: pre"> </SPAN>boolean flag = xmlPullService.save(persons1);
<SPAN style="WHITE-SPACE: pre"> </SPAN>if (flag) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.makeText(XmlActivity.this, "采用pull解析生產xml文件成功",
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.LENGTH_LONG).show();
<SPAN style="WHITE-SPACE: pre"> </SPAN>} else {
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.makeText(XmlActivity.this, "采用pull解析生產xml文件失敗",
<SPAN style="WHITE-SPACE: pre"> </SPAN>Toast.LENGTH_LONG).show();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>} catch (Exception e) {
<SPAN style="WHITE-SPACE: pre"> </SPAN>// TODO Auto-generated catch block
<SPAN style="WHITE-SPACE: pre"> </SPAN>e.printStackTrace();
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>default:
<SPAN style="WHITE-SPACE: pre"> </SPAN>break;
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
<SPAN style="WHITE-SPACE: pre"> </SPAN>}
}
package com.example.lession03_xml;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.example.lession03_xml.domain.Person;
import com.example.lession03_xml.service.XMLContentHandlerService;
import com.example.lession03_xml.service.XMLDomService;
import com.example.lession03_xml.service.XMLPullService;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class XmlActivity extends Activity {
// 聲明控件
public Button btn_sax, btn_dom, btn_pull, btn_cpull;
public XMLDomService xmlDomService;
public XMLPullService xmlPullService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設置顯示的視圖
setContentView(R.layout.activity_xml);
// 實例化
xmlDomService = new XMLDomService();
xmlPullService = new XMLPullService();
// 通過findViewById方法獲取控件對象
btn_sax = (Button) findViewById(R.id.btn_sax);
btn_dom = (Button) findViewById(R.id.btn_dom);
btn_pull = (Button) findViewById(R.id.btn_pull);
btn_cpull = (Button) findViewById(R.id.button_cpull);
// 給按鈕注冊事件
btn_sax.setOnClickListener(new MyClickListener());
btn_dom.setOnClickListener(new MyClickListener());
btn_pull.setOnClickListener(new MyClickListener());
btn_cpull.setOnClickListener(new MyClickListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.xml, menu);
return true;
}
class MyClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// 獲取組件的id
int id = v.getId();
switch (id) {
case R.id.btn_sax:
Toast.makeText(XmlActivity.this, "采用sax解析xml文件案例",
Toast.LENGTH_LONG).show();
// sax解析的工廠對象
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// 得到sax的解析器
SAXParser saxParser = factory.newSAXParser();
// 創建handler對象
XMLContentHandlerService handlerService = new XMLContentHandlerService();
// 獲取到了 asserts目錄中 csdn.xml文件
InputStream is = getAssets().open("csdn.xml");
// 直接解析 InputStream的流對象
saxParser.parse(is, handlerService);
// 通過 handlerService對象
Toast.makeText(XmlActivity.this,
"---" + handlerService.getPersons().size(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.btn_dom:
InputStream is = null;
try {
// 獲取讀取文件的輸入流對象
is = getAssets().open("csdn.xml");
// 采用
List<Person> persons = xmlDomService.parseXML(is);
//
Toast.makeText(XmlActivity.this,
"" + persons.get(0).getName(), Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.btn_pull:
List<Person> persons = null;
try {
persons = xmlPullService.getPerson(getAssets().open(
"csdn.xml"));
} catch (Exception e) {
e.printStackTrace();
}
if (persons != null) {
Toast.makeText(
XmlActivity.this,
"采用pull解析xml文件" + persons.get(0).getName() + "--"
+ persons.get(1).getName(),
Toast.LENGTH_LONG).show();
}
break;
case R.id.button_cpull:
// 重新生成一個xml文件 mycsdn.xml
List<Person> persons1 = null;
try {
persons1 = xmlPullService.getPerson(getAssets().open(
"csdn.xml"));
// 重新生成一個新xml文件
boolean flag = xmlPullService.save(persons1);
if (flag) {
Toast.makeText(XmlActivity.this, "采用pull解析生產xml文件成功",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(XmlActivity.this, "采用pull解析生產xml文件失敗",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
}
}
一、Android Studio簡單介紹2013年GoogleI/O大會首次發布了Android Studio IDE(Android平台集成開發環境)。它基於Intel
目前智能電視終端(智能電視和智能電視盒子)已經越來越火,過去主打視頻功能,如今的智能電視終端不僅會繼續完善視頻功能,還會加入電視游戲功能,同時這也趕上了“電視游戲機解禁”
最近由於項目需要,寶寶好久沒搞Android啦,又是因為項目需要,現在繼續弄Android,哎,說多了都是淚呀,別的不用多說,先搞一個登錄界面練練手,登錄界面可以說是An
一、 I2C簡介 I2C(Inter-Integrated Circuit)總線是一種由 Philips 公司開發的兩線式串行總線,用於連接微控制器及其外圍設備。I2