您的位置:首页 > 其它

使用Sax和Pull方法 解析XML文件

2015-09-19 08:59 211 查看

xml 可扩展标签语言
结构良好的xml文档
1.标签成对出现
2.根元素有且只有一个
3.大小写敏感
4.不能嵌套和交叉
5.属性要用双引号或者单引号
6.包含xml声明

解析MXL文件
Sax解析的步骤

1.创建工厂 SaxParserFactory
2.创建解析器 AaxParser
3.创建读取器XmlReader
4.设置时间处理器 SetContentHandler();
5.解析 parse(“路径”)

额外:  创建事件处理器

例 : books.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category='java'>
<title lang="english">java基础</title>
<author>gosling</author>
<year>2014</year>
<price>200</price>
</book>
<book category="android">
<title lang="chinese">android基础</title>
<author>google</author>
<year>2014</year>
<price>20</price>
</book>
</bookstore>
书籍类
public class Book {

private int id;
private String bookname;
private String author;
private double price;

public Book() {
// TODO Auto-generated constructor stub
}

public Book(int id, String bookname, String author, double price) {
super();
this.id = id;
this.bookname = bookname;
this.author = author;
this.price = price;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getBookname() {
return bookname;
}

public void setBookname(String bookname) {
this.bookname = bookname;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public void showInfo(){
System.out.println("id:"+id+"  bookname:"+bookname+"   author:"+author+"  price:"+price);
}

}
用SAX解析
@需求分析: 要求:把 books.xml文件中的书籍信息封装成对象,然后放入集合中
Public class SAXMain
{
public static void main(String[]args) throws Exception{
//创建工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//2.创建解析器
SAXParser parser = factory.newSAXParser();
//3.获取xmlreader
XMLReader reader = parser.getXMLReader();
//4.设置处理器
BookContentHandler handler=new BookContentHandler();

//5.解析
reader.parser("xml\\books.xml");
//6.遍历集合

}
}
//事件处理器
class BookContentHandler extends DefaultHandler{
ArraryList<Book> books = new ArrayList<Book>();
Book book=null;
String currentTagName;//当前读到的开始标签
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
//把当前读到的标签设置到currentTagName
currentTagName = qName;
if(qName.equals("book"))
{
book = new Bok();
//获取id
String id=attributes.getValue("id");
book.setID(Integer.parseInt(id));
}
public void endElement(String uri, String localName, String qName)throws SAXException {
currentTagName="";//跳过空格和换行
if(qName.equals("book"))
{
books.add(book);//添加到集合
book=null;
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
//判断bookname
if(currentTagName.equals("bookname"))
{
book.setBookname(new String(ch,start,length));
}else if(currentTagName.equals("author"))
{
book.setAuthor(new String(ch,start,length));
}else if(currentTagName.equals("price"))
{       double price=Double.parserDouble(new String(ch,start,length))
book.setPrice(price);
}

}
}
}

PULl解析
1 创建工厂  XmlPullParserFactory
2 创建解析器  XmlPullParser
3 设置读取的文件流
4 获取第一个事件类型
5 while循环读取  type=parser.next(); 下一个类型    parser.getName() 获取标签名称   parser.nextText(); 获取标签中的内容

@需求分析: 使用PULL方式解析books.xml ,封装成书对象,然后放入集合
public class PullMain{
public static void main(String[]args) throws Exception{
//1.创建工厂
XmlPullParserFactory factory=XmlPullParserFactory .newInstance();
//2.创建解析器
XmlPullParser parser = factory.newPullParser();
//3.设置要读取的xml文件,并指定编码
parer.setInput(new FileInputStream("xml\\books.xml"),"gbk");
//4.获取第一个事件类型
int type=parser.getEventType();
ArrayList<Book>books=new ArraryList<book>();
Book book=null;
while(type!=XmlPullParser.END_DOCUMENT)
{
switch(type)
{
case  XmlPullParser.STATR_TAG:
//getName()获取标签名称
if(parser.getName().equals("book"))
{
book = new Book();
//获取id
String id=parser.getAttributeValue(null,"id");
book.setId(Integer.parserInt(id));
}else if(parser.getName().equals("bookname"))
{
book.setBookname(parser.nextText());
}else if(parser.getName().equals("author"))
{
book.setAuthor(parser.nextText());
}else if (parser.getName().equals("price")) {
String price = parser.nextText();
book.setPrice(Double.parseDouble(price));
}
break;
case XmlPullParser.END_TAG:// 3
//当读取book结束标记的时候 ,添加到集合中
if (parser.getName().equals("book")) {
books.add(book);
}
break;
}
//获取下一个事件
type = parser.next();
}
for (Book b : books) {
b.showInfo();
}

}

}
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: