編輯:關於Android編程
除了可以使用 SAX和DOM解析XML文件,大家也可以使用Android內置的Pull解析器解析XML文件。
Pull解析器的運行方式與 SAX 解析器相似。
它提供了類似的事件,如:開始元素和結束元素事件,使用parser.next()可以進入下一個元素並觸發相應事件。
事件將作為數值代碼被發送,因此可以使用一個switch對感興趣的事件進行處理。
當元素開始解析時,調用parser.nextText()方法可以獲取下一個Text類型元素的值。
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> </SPAN>public List<Person> getPerson(InputStream inputStream) throws Exception {
<SPAN> </SPAN>List<Person> persons = null;
<SPAN> </SPAN>Person person = null;
<SPAN> </SPAN>// android提供一個類XmlPullParser,得到pull解析器
<SPAN> </SPAN>XmlPullParser pullParser = Xml.newPullParser();
<SPAN> </SPAN>// 設置要進行解析的內容,不是立刻發生,調用才發生
<SPAN> </SPAN>pullParser.setInput(inputStream, "utf-8");
<SPAN> </SPAN>/* <?xml version="1.0" encoding="UTF-8"?> */
<SPAN> </SPAN>// 觸發第一個事件 用數字形式表示 開始文檔事件
<SPAN> </SPAN>int event = pullParser.getEventType();
<SPAN> </SPAN>// 不等於文檔的結束時間
<SPAN> </SPAN>while (event != XmlPullParser.END_DOCUMENT) {
<SPAN> </SPAN>// 屬於開始文檔事件
<SPAN> </SPAN>switch (event) {
<SPAN> </SPAN>case XmlPullParser.START_DOCUMENT:
<SPAN> </SPAN>persons = new ArrayList<Person>();
<SPAN> </SPAN>break;
<SPAN> </SPAN>case XmlPullParser.START_TAG:
<SPAN> </SPAN>/* <persons> */
<SPAN> </SPAN>if ("person".equals(pullParser.getName())) {
<SPAN> </SPAN>/* <person id="23"> */
<SPAN> </SPAN>int id = new Integer(pullParser.getAttributeValue(0));
<SPAN> </SPAN>person = new Person();
<SPAN> </SPAN>person.setId(id);
<SPAN> </SPAN>}
<SPAN> </SPAN>if (person != null) {
<SPAN> </SPAN>/* <name> */
<SPAN> </SPAN>if ("name".equals(pullParser.getName())) {
<SPAN> </SPAN>// 返回文本的值
<SPAN> </SPAN>/* 李明 */
<SPAN> </SPAN>person.setName(pullParser.nextText());
<SPAN> </SPAN>}
<SPAN> </SPAN>/* <age> */
<SPAN> </SPAN>if ("age".equals(pullParser.getName())) {
<SPAN> </SPAN>/* 30 */person.setAge(new Short(pullParser.nextText()));
<SPAN> </SPAN>}
<SPAN> </SPAN>}
<SPAN> </SPAN>break;
<SPAN> </SPAN>// 結束元素事件
<SPAN> </SPAN>/* </persons> */
<SPAN> </SPAN>case XmlPullParser.END_TAG:
<SPAN> </SPAN>// 當前解析器指向的節點為person
<SPAN> </SPAN>if ("person".equals(pullParser.getName())) {
<SPAN> </SPAN>persons.add(person);
<SPAN> </SPAN>person = null;
<SPAN> </SPAN>}
<SPAN> </SPAN>break;
<SPAN> </SPAN>}
<SPAN> </SPAN>// 解析器向後解析
<SPAN> </SPAN>event = pullParser.next();
<SPAN> </SPAN>}
<SPAN> </SPAN>return persons;
<SPAN> </SPAN>}
<SPAN> </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;
}
}
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> </SPAN>// 聲明控件
<SPAN> </SPAN>public Button btn_sax, btn_dom, btn_pull, btn_cpull;
<SPAN> </SPAN>public XMLDomService xmlDomService;
<SPAN> </SPAN>public XMLPullService xmlPullService;
<SPAN> </SPAN>@Override
<SPAN> </SPAN>protected void onCreate(Bundle savedInstanceState) {
<SPAN> </SPAN>super.onCreate(savedInstanceState);
<SPAN> </SPAN>// 設置顯示的視圖
<SPAN> </SPAN>setContentView(R.layout.activity_xml);
<SPAN> </SPAN>// 實例化
<SPAN> </SPAN>xmlDomService = new XMLDomService();
<SPAN> </SPAN>xmlPullService = new XMLPullService();
<SPAN> </SPAN>// 通過findViewById方法獲取控件對象
<SPAN> </SPAN>btn_sax = (Button) findViewById(R.id.btn_sax);
<SPAN> </SPAN>btn_dom = (Button) findViewById(R.id.btn_dom);
<SPAN> </SPAN>btn_pull = (Button) findViewById(R.id.btn_pull);
<SPAN> </SPAN>btn_cpull = (Button) findViewById(R.id.button_cpull);
<SPAN> </SPAN>// 給按鈕注冊事件
<SPAN> </SPAN>btn_sax.setOnClickListener(new MyClickListener());
<SPAN> </SPAN>btn_dom.setOnClickListener(new MyClickListener());
<SPAN> </SPAN>btn_pull.setOnClickListener(new MyClickListener());
<SPAN> </SPAN>btn_cpull.setOnClickListener(new MyClickListener());
<SPAN> </SPAN>}
<SPAN> </SPAN>@Override
<SPAN> </SPAN>public boolean onCreateOptionsMenu(Menu menu) {
<SPAN> </SPAN>// Inflate the menu; this adds items to the action bar if it is present.
<SPAN> </SPAN>getMenuInflater().inflate(R.menu.xml, menu);
<SPAN> </SPAN>return true;
<SPAN> </SPAN>}
<SPAN> </SPAN>class MyClickListener implements View.OnClickListener {
<SPAN> </SPAN>@Override
<SPAN> </SPAN>public void onClick(View v) {
<SPAN> </SPAN>// 獲取組件的id
<SPAN> </SPAN>int id = v.getId();
<SPAN> </SPAN>switch (id) {
<SPAN> </SPAN>case R.id.btn_sax:
<SPAN> </SPAN>Toast.makeText(XmlActivity.this, "采用sax解析xml文件案例",
<SPAN> </SPAN>Toast.LENGTH_LONG).show();
<SPAN> </SPAN>// sax解析的工廠對象
<SPAN> </SPAN>SAXParserFactory factory = SAXParserFactory.newInstance();
<SPAN> </SPAN>try {
<SPAN> </SPAN>// 得到sax的解析器
<SPAN> </SPAN>SAXParser saxParser = factory.newSAXParser();
<SPAN> </SPAN>// 創建handler對象
<SPAN> </SPAN>XMLContentHandlerService handlerService = new XMLContentHandlerService();
<SPAN> </SPAN>// 獲取到了 asserts目錄中 csdn.xml文件
<SPAN> </SPAN>InputStream is = getAssets().open("csdn.xml");
<SPAN> </SPAN>// 直接解析 InputStream的流對象
<SPAN> </SPAN>saxParser.parse(is, handlerService);
<SPAN> </SPAN>// 通過 handlerService對象
<SPAN> </SPAN>Toast.makeText(XmlActivity.this,
<SPAN> </SPAN>"---" + handlerService.getPersons().size(),
<SPAN> </SPAN>Toast.LENGTH_LONG).show();
<SPAN> </SPAN>} catch (Exception e) {
<SPAN> </SPAN>// TODO Auto-generated catch block
<SPAN> </SPAN>e.printStackTrace();
<SPAN> </SPAN>}
<SPAN> </SPAN>break;
<SPAN> </SPAN>case R.id.btn_dom:
<SPAN> </SPAN>InputStream is = null;
<SPAN> </SPAN>try {
<SPAN> </SPAN>// 獲取讀取文件的輸入流對象
<SPAN> </SPAN>is = getAssets().open("csdn.xml");
<SPAN> </SPAN>// 采用
<SPAN> </SPAN>List<Person> persons = xmlDomService.parseXML(is);
<SPAN> </SPAN>//
<SPAN> </SPAN>Toast.makeText(XmlActivity.this,
<SPAN> </SPAN>"" + persons.get(0).getName(), Toast.LENGTH_LONG)
<SPAN> </SPAN>.show();
<SPAN> </SPAN>} catch (Exception e) {
<SPAN> </SPAN>e.printStackTrace();
<SPAN> </SPAN>}
<SPAN> </SPAN>break;
<SPAN> </SPAN>case R.id.btn_pull:
<SPAN> </SPAN>List<Person> persons = null;
<SPAN> </SPAN>try {
<SPAN> </SPAN>persons = xmlPullService.getPerson(getAssets().open(
<SPAN> </SPAN>"csdn.xml"));
<SPAN> </SPAN>} catch (Exception e) {
<SPAN> </SPAN>e.printStackTrace();
<SPAN> </SPAN>}
<SPAN> </SPAN>if (persons != null) {
<SPAN> </SPAN>Toast.makeText(
<SPAN> </SPAN>XmlActivity.this,
<SPAN> </SPAN>"采用pull解析xml文件" + persons.get(0).getName() + "--"
<SPAN> </SPAN>+ persons.get(1).getName(),
<SPAN> </SPAN>Toast.LENGTH_LONG).show();
<SPAN> </SPAN>}
<SPAN> </SPAN>break;
<SPAN> </SPAN>case R.id.button_cpull:
<SPAN> </SPAN>
<SPAN> </SPAN>break;
<SPAN> </SPAN>default:
<SPAN> </SPAN>break;
<SPAN> </SPAN>}
<SPAN> </SPAN>}
<SPAN> </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:
break;
default:
break;
}
}
}
}
最近的一些學習心得:功能實現:點擊圓形頭像之後可以實現相冊上傳或者開啟相機,然後把得到的圖片經過剪裁,把剪裁過的圖片設置為頭像的背景圖步驟:第一步:自定義一個類,繼承Im
相信一些有關注魅族的用戶都知道魅族即將要發布一款新機為魅族mx6,那麼魅族mx6這款新機怎麼樣?魅族mx6跑分多少呢?下文帶來魅族mx6性能跑分評測,一起和
先上幾張效果圖:在加載多圖片時,我們采用後進先出策略(即滑動到哪裡就先加載哪裡的圖片),節省了內存的使用,也有了更好的用戶體驗。接著我們就先定義自己的ImageLoade
ViewRoot目前這個類已經沒有了,是老版本中的一個類,在Android2.2以後用ViewRootImpl代替ViewRoot,對應於ViewRootImpl.jav