1.Sax解析xml文件
Sax解析读取一点,解析一点,适合读取大容量的xml文件,从上往下逐点读取,不能往回读,Sax解析通常只读取文件,不能修改文件。
1.1将user.xml传到assets中
<?xml version="1.0"encoding="UTF-8"?>
<list>
<user>
<username>root</username>
<password>root</password>
<email>[email protected]</email>
</user>
<user>
<username>张三丰</username>
<password>zhangsanfeng</password>
<email>[email protected]</email>
</user>
</list>
1.2新建Class对标签(username,password,email)进行私有定义化
package com.example.day20190530;
public class User {
private String username;
private String password;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
1.3.1创建解析器
Sax解释方式的步骤
1.先要创建Sax解释器的工厂对象
2.可以使用Sax解释器的工厂对象生产一个Sax解释器
3.创建事件处理器类,编写对应的读取方式
4.使用Sax解释器解释xml文件
package com.example.day20190530;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SaxActivity extends Activity implements OnClickListener {
private Button saxBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sax);
saxBtn = (Button) findViewById(R.id.saxBtn);
saxBtn.setOnClickListener(this); }
@Override
public void onClick(View v) {
try {
// 读取assets目录中的资源文件
InputStream is = getAssets().open("user.xml");
// Xml解析器
SaxHelper helper = new SaxHelper();
// Sax解析器工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 创建Sax解析器
SAXParser parser = factory.newSAXParser();
// 将helper分配给Sax解析器
parser.parse(is, helper);
is.close();
Toast.makeText(this, "共有" + helper.getList().size() + "条数据", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.3.2创建事件处理器
package com.example.day20190530;
import java.util.ArrayList; import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
/**
* Sax解析器
* @author Administrator
* */
public class SaxHelper extends DefaultHandler {
private List<User> list;// 存放解析的所有对象
private User user;
private String tagName;// 解析的标签的名字
/**
* 解析文档开始执行的方法
*/
@Override
public void startDocument() throws SAXException {
list = new ArrayList<User>();
Log.i("sax", "开始解析文档");
}
/**
* 解析节点开始执行的方法
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("user".equals(localName)) {
user = new User();
Log.i("sax", "开始解析" + localName + "节点");
}
this.tagName = localName;
}
/**
* 读取节点内容
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (tagName != null) {
String data = new String(ch, start, length);
if ("username".equals(tagName)) {
user.setUsername(data);
} else if ("password".equals(tagName)) {
user.setPassword(data);
} else if ("email".equals(tagName)) {
user.setEmail(data);
}
Log.i("sax", "读取节点的内容" + data);
}
}
/**
* 读取结束节点
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if("user".equals(localName)){
list.add(user);
user=null;
Log.i("sax", "结束节点"+localName);
}
tagName=null;
}
/**
* 文档解析结束
*/
@Override
public void endDocument() throws SAXException {
Log.i("sax","文档解析结束");
}
/**
* 返回list集合
* @return
*/
public List<User>getList(){
return list;
}
}
2.Dom解析文件
2.1将user.xml文件导入到assets中
2.2创建Class将(password,username,email)私有定义化
2.3创建Dom解析器
package com.example.day20190530;
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.NodeList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; import android.widget.Button;
import android.widget.Toast;
public class DomActivity extends Activity implements OnClickListener {
private Button domBtn;
private User user;
private List<User> list;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dom);
list = new ArrayList<User>();
domBtn = (Button) findViewById(R.id.domBtn);
domBtn.setOnClickListener(this);
}
@Override public void onClick(View v) {
// 创建Dom解析工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建Dom解析器
try {
DocumentBuilder builder = factory.newDocumentBuilder();
// 读取资源文件
InputStream is = getAssets().open("user.xml");
// 将文档对象加载到内存中
Document doc = builder.parse(is);
// 找到所有的user节点
NodeList userList = doc.getElementsByTagName("user");
// 遍历所有的user节点和子节点
for (int i = 0; i < userList.getLength(); i++) {
// 获取每一个user节点
Element eleUser = (Element) userList.item(i);
// 获取子节点的内容
String username = eleUser.getElementsByTagName("username").item(0).getFirstChild().getNodeValue();
String passeord = eleUser.getElementsByTagName("passeord").item(0).getFirstChild().getNodeValue();
String email = eleUser.getElementsByTagName("email").item(0).getFirstChild().getNodeValue();
Toast.makeText(this, username + "," + passeord + "," + email, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.Full解析xml文件
3.1将user.xml文件导入到assets目录中
3.2创建Class将(password,username,email)进行私有定义化
3.3创建Full解析器
package com.example.day20190530;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; import android.widget.Button;
import android.widget.Toast;
public class FullActivity extends Activity implements OnClickListener {
private Button pullBtn;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full);
pullBtn = (Button) findViewById(R.id.pullBtn);
pullBtn.setOnClickListener(this);
}
@Override public void onClick(View v) {
try {
// 读取资源文件
InputStream is = getAssets().open("book.xml");
// 创建解析器工厂对象
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, "UTF-8");// 设置解析编码
int eventType = parser.getEventType();// 获取事件类型
// 判断文档是否读取结束
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagName = parser.getName();// 获取标签名
// 判断是否为开始标签
if (eventType == XmlPullParser.START_TAG) {
if ("name".equals(tagName)) {
Toast.makeText(this, parser.nextText(),Toast.LENGTH_SHORT).show();
} else if ("price".equals(tagName)) {
Toast.makeText(this, parser.nextText(),Toast.LENGTH_SHORT).show();
} else if ("desc".equals(tagName)) {
Toast.makeText(this, parser.nextText(),Toast.LENGTH_SHORT).show();
}
}
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文地址:https://www.cnblogs.com/zhulinglin/p/zhuzhu.html