package alice.bw.com.day10xmljiexi;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) throws XmlPullParserException, IOException, ParserConfigurationException, SAXException {
switch (view.getId()){
case R.id.pull_btn:
pullXMlstuJieXi();//pull解析
break;
case R.id.pull_phone_btn:
pullXmlPhoneJieXi();//pull解析 带属性
break;
case R.id.Dom_btn:
domXmlStudentJieXi();//dom解析
break;
case R.id.sax_btn:
saxXmlStudentJieXi();//sax解析
break;
}
}
private void domXmlStudentJieXi() throws ParserConfigurationException, IOException, SAXException {
List<Student> students = new ArrayList<>();
//获取DOM解析器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获DOM解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//将解析树放入内存,通过返回值Document来描述结果
Document document = builder.parse(getResources().getAssets().open("Student.xml"));
//取得根元素<</Students>>
Element root = document.getDocumentElement();
//取得所有student节点集合
NodeList personNodes = root.getElementsByTagName("student");
for (int i = 0; i < personNodes.getLength(); i++) {
Student stu = new Student();
//取得student节点元素
Element personElement = (Element) personNodes.item(i);
//取得属性值并设置ID
// stu.setNo(personElement.getAttribute("no"));
// stu.setName(personElement.getAttribute("name"));
// stu.setAge(Integer.parseInt(personElement.getAttribute("age")));
//获取person的子节点
NodeList personChilds = personElement.getChildNodes();
for (int j = 0; j < personChilds.getLength(); j++) {
//判断当前节点是否是元素类型的节点
if (personChilds.item(j).getNodeType() == Node.ELEMENT_NODE) {
Element childElement = (Element) personChilds.item(j);
String nodeName = childElement.getNodeName();
switch (nodeName){
case "no":
//获取孙节点的值
stu.setNo(childElement.getFirstChild().getNodeValue());
break;
case "name":
stu.setName(childElement.getFirstChild().getNodeValue());
break;
case "age":
stu.setAge(Short.parseShort(childElement.getFirstChild().getNodeValue()));
break;
}
}
}
students.add(stu);
}
for (Student stu:students){
Log.d("sxl", "domXmlStudentJieXi: "+stu);
}
}
private void saxXmlStudentJieXi() throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
MyHandler mh = new MyHandler();
sp.parse(getResources().getAssets().open("Student.xml"), mh);
ArrayList<Student> list = mh.list;
for (Student student : list) {
Log.d("sxl", "saxXmlStudentJieXi: "+student);
}
}
private void pullXmlPhoneJieXi() throws XmlPullParserException, IOException {
//解析工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//解析器
XmlPullParser parser = factory.newPullParser();
Phone phone = null;
int e = XmlPullParser.START_DOCUMENT;
ArrayList<Phone> list = new ArrayList<Phone>();
parser.setInput(getResources().getAssets().open("Phone.xml"), "UTF-8");
String name;
while (e!=XmlPullParser.END_DOCUMENT) {
name = parser.getName();
switch (e) {
case XmlPullParser.START_TAG:
switch (name) {
case "phone":
phone = new Phone();
for (int i = 0; i < parser.getAttributeCount(); i++) {
switch (parser.getAttributeName(i)) {
case "name":
phone.setName(parser.getAttributeValue(i));
break;
case "price":
phone.setPrice(Integer.parseInt(parser.getAttributeValue(i)));
break;
}
}
break;
case "RAM":
phone.setARM(Integer.parseInt(parser.nextText()));
break;
}
break;
case XmlPullParser.END_TAG:
if(name.equals("phone")){
list.add(phone);
}
break;
}
e = parser.next();
}
for (Phone phone2 : list) {
Log.d("sxl", "pullXmlPhoneJieXi: "+phone2);
}
}
private void pullXMlstuJieXi() throws XmlPullParserException, IOException {
XmlPullParserFactory xpf = XmlPullParserFactory.newInstance();
//解析器
XmlPullParser xpp = xpf.newPullParser();
xpp.setInput(getResources().getAssets().open("Student.xml"),"UTF-8");
ArrayList<Student> list = new ArrayList<Student>();
//获得事件类型
int e = xpp.getEventType();
Student stu=null;
while (e!=XmlPullParser.END_DOCUMENT) {
switch (e) {
case XmlPullParser.START_TAG:
String name = xpp.getName();
switch (name) {
case "student":
stu= new Student();
break;
case "no":
stu.setNo(xpp.nextText());
break;
case "name":
stu.setName(xpp.nextText());
break;
case "age":
stu.setAge(Integer.parseInt(xpp.nextText()));
break;
}
break;
case XmlPullParser.END_TAG:
String name1 = xpp.getName();
switch (name1) {
case "student":
list.add(stu);
break;
}
break;
}
//移动到下一个
e = xpp.next();
}
for (Student s : list) {
Log.d("sxl", "pullXMlJieXi: "+s);
}
}
}
MyHanlder
package alice.bw.com.day10xmljiexi;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
/**
* 定义解析标签
*/
public class MyHandler extends DefaultHandler{
ArrayList<Student> list;
Student stu;
String tageName;//用来记录正在解析的标签
/**
* 取出标签中的文本
* ch:标签中文本的char数据集合
* start:开始读取位置
* length:读取长度
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
switch (tageName) {
case "no":
stu.setNo(new String(ch, start, length));
break;
case "name":
stu.setName(new String(ch, start, length));
break;
case "age":
stu.setAge(Integer.parseInt(new String(ch, start, length)));
break;
}
}
/**
* 标签开始时调用该方法
* 一般在该方法中创建对象,取出属性值,将得到的标签名赋值给定义的标签名
* qName:正在解析的标签名
* attributes:正在解析的标签属性集合
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equals("student")){
stu = new Student();
}
tageName = qName;
}
/**
* 标签结束时调用
* 将对象存放进集合中
* 将定义的正在解析的标签制空,保证换行或回车等不必要的数据导致解析数据错乱
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equals("student")){
list.add(stu);
}
tageName ="";
}
/**
* 文件开始解析时调用
* 一般在该方法中初始化集合
*/
@Override
public void startDocument() throws SAXException {
list = new ArrayList<Student>();
}
/**
* 文件解析结束时调用
* 不做操作
*/
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}
类
package alice.bw.com.day10xmljiexi;
public class Phone {
private String name;
private int price;
private int ARM;
public Phone(String name, int price, int aRM) {
super();
this.name = name;
this.price = price;
ARM = aRM;
}
public Phone() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getARM() {
return ARM;
}
public void setARM(int aRM) {
ARM = aRM;
}
@Override
public String toString() {
return "Phone [name=" + name + ", price=" + price + ", ARM=" + ARM + "]";
}
}
package alice.bw.com.day10xmljiexi;
public class Student {
private String no;
private String name;
private int age;
public Student(String no, String name, int age) {
super();
this.no = no;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [no=" + no + ", name=" + name + ", age=" + age + "]";
}
public Student() {
super();
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}