Android中Sax解析与Dom解析xml文件
2013-05-30 20:52:59 来源:WEB开发网核心提示:-package com.example.day03_xml.service;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;impo
-----------------------------------------------------------------------------------------------------------------------------
package com.example.day03_xml.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.example.day03_xml.domain.Person;
public class XMLDomService {
public List<Person> parseXML(InputStream is) {
List<Person> list = new ArrayList<Person>();
// 创建DOM工厂对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// DocumentBuilder对象
DocumentBuilder builder = factory.newDocumentBuilder();
// 获取文档对象
Document document = builder.parse(is);
// 获取文档对象的root
Element root = document.getDocumentElement();
// 获取persons根节点中所有的person节点对象
NodeList personNodes = root.getElementsByTagName("person");
// 遍历所有的person节点
for (int i = 0; i < personNodes.getLength(); i++) {
Person person = new Person();
// 根据item(index)获取该索引对应的节点对象
Element personNode = (Element) personNodes.item(i); // 具体的person节点
// 设置id属性值
person.setId(Integer.parseInt(personNode.getAttribute("id")));
// 获取该节点下面的所有字节点
NodeList personChildNodes = personNode.getChildNodes();
// 遍历person的字节点
for (int index = 0; index < personChildNodes.getLength(); index++) {
// 获取子节点
Node node = personChildNodes.item(index);
// 判断node节点是否是元素节点
if (node.getNodeType() == Node.ELEMENT_NODE) {
// 把节点转换成元素节点
Element element = (Element) node;
// 判断元素节点是否是name元素节点
if ("name".equals(element.getNodeName())) {
person.setName(element.getFirstChild()
.getNodeValue());
} else if ("age".equals(element.getNodeName())) {
person.setAge(new Short(element.getFirstChild()
.getNodeValue()));
}
}
}
// 把person对象加入到集合中
list.add(person);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.example.day03_xml.domain.Person;
public class XMLDomService {
public List<Person> parseXML(InputStream is) {
List<Person> list = new ArrayList<Person>();
// 创建DOM工厂对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// DocumentBuilder对象
DocumentBuilder builder = factory.newDocumentBuilder();
// 获取文档对象
Document document = builder.parse(is);
// 获取文档对象的root
Element root = document.getDocumentElement();
// 获取persons根节点中所有的person节点对象
NodeList personNodes = root.getElementsByTagName("person");
// 遍历所有的person节点
for (int i = 0; i < personNodes.getLength(); i++) {
Person person = new Person();
// 根据item(index)获取该索引对应的节点对象
Element personNode = (Element) personNodes.item(i); // 具体的person节点
// 设置id属性值
person.setId(Integer.parseInt(personNode.getAttribute("id")));
// 获取该节点下面的所有字节点
NodeList personChildNodes = personNode.getChildNodes();
// 遍历person的字节点
for (int index = 0; index < personChildNodes.getLength(); index++) {
// 获取子节点
Node node = personChildNodes.item(index);
// 判断node节点是否是元素节点
if (node.getNodeType() == Node.ELEMENT_NODE) {
// 把节点转换成元素节点
Element element = (Element) node;
// 判断元素节点是否是name元素节点
if ("name".equals(element.getNodeName())) {
person.setName(element.getFirstChild()
.getNodeValue());
} else if ("age".equals(element.getNodeName())) {
person.setAge(new Short(element.getFirstChild()
.getNodeValue()));
}
}
}
// 把person对象加入到集合中
list.add(person);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
package com.example.day03_xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.example.day03_xml.domain.Person;
import com.example.day03_xml.service.XMLContentHandlerService;
import com.example.day03_xml.service.XMLDomService;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
public Button btn_sax, btn_dom, btn_pull;
public XMLDomService xmlDomService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示的视图
setContentView(R.layout.activity_main);
// 实例化
xmlDomService = new XMLDomService();
// 通过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_sax.setOnClickListener(new MyClickListener());
btn_dom.setOnClickListener(new MyClickListener());
btn_pull.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.main, menu);
return true;
}
class MyClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_sax:
Toast.makeText(MainActivity.this, "采用sax解析xml",
Toast.LENGTH_LONG).show();
// sax解析的工厂对象
SAXParserFactory factory = SAXParserFactory.newInstance();
// 得到sax的解析器
try {
SAXParser saxParser = factory.newSAXParser();
// 创建handler对象
XMLContentHandlerService handlerService = new XMLContentHandlerService();
// 获取到了aserts目录中的csdn.xml文件
InputStream is = getAssets().open("csdn.xml");
// 直接解析 InputStream的流对象
saxParser.parse(is, handlerService);
// 通过 handlerService对象
Toast.makeText(MainActivity.this,
"---" + handlerService.getPersons().size(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.btn_dom:
Toast.makeText(MainActivity.this, "采用dom解析xml",
Toast.LENGTH_LONG).show();
InputStream is = null;
try {
is = getAssets().open("csdn.xml");
List<Person> persons = xmlDomService.parseXML(is);
Toast.makeText(MainActivity.this,
"" + persons.get(0).getName(), Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case R.id.btn_pull:
Toast.makeText(MainActivity.this, "采用pull解析xml",
Toast.LENGTH_LONG).show();
break;
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.example.day03_xml.domain.Person;
import com.example.day03_xml.service.XMLContentHandlerService;
import com.example.day03_xml.service.XMLDomService;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
public Button btn_sax, btn_dom, btn_pull;
public XMLDomService xmlDomService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示的视图
setContentView(R.layout.activity_main);
// 实例化
xmlDomService = new XMLDomService();
// 通过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_sax.setOnClickListener(new MyClickListener());
btn_dom.setOnClickListener(new MyClickListener());
btn_pull.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.main, menu);
return true;
}
class MyClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_sax:
Toast.makeText(MainActivity.this, "采用sax解析xml",
Toast.LENGTH_LONG).show();
// sax解析的工厂对象
SAXParserFactory factory = SAXParserFactory.newInstance();
// 得到sax的解析器
try {
SAXParser saxParser = factory.newSAXParser();
// 创建handler对象
XMLContentHandlerService handlerService = new XMLContentHandlerService();
// 获取到了aserts目录中的csdn.xml文件
InputStream is = getAssets().open("csdn.xml");
// 直接解析 InputStream的流对象
saxParser.parse(is, handlerService);
// 通过 handlerService对象
Toast.makeText(MainActivity.this,
"---" + handlerService.getPersons().size(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.btn_dom:
Toast.makeText(MainActivity.this, "采用dom解析xml",
Toast.LENGTH_LONG).show();
InputStream is = null;
try {
is = getAssets().open("csdn.xml");
List<Person> persons = xmlDomService.parseXML(is);
Toast.makeText(MainActivity.this,
"" + persons.get(0).getName(), Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case R.id.btn_pull:
Toast.makeText(MainActivity.this, "采用pull解析xml",
Toast.LENGTH_LONG).show();
break;
}
}
}
}
更多精彩
赞助商链接