Sax的兩種解析方法

第一種:
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/*
 * 要解析的XML中的內容,根據xml創建javabean
 * <?xml version="1.0" encoding="utf-8"?>
<Student>
   <student>
      <id>1</id>
      <name>張三</name>
      <age>22</age>
   </student>
   <student>
      <id>2</id>
      <name>李四</name>
      <age>20</age>
   </student>
</Student>
 */
public class SaxHandle1 extends  DefaultHandler{
    private List<Student> list;//用來存放數據
    Student stu;
    String tag;  //tag用來保存XML文檔的開始或結束標記名
  //返回一個集合供測試類調用
    public List<Student> getList(){
    return list;
    }
@Override
public void startDocument() throws SAXException {
System.out.println("開始文檔解析");
list=new ArrayList<Student>();//開始就立刻建立集合
//表示兩個<sutdent></student>中的內容,只作新建建集合
}
@Override
public void endDocument() throws SAXException {
System.out.println("文檔解析結束");
}
    //開始讀取文檔的標記,好比xml中自定義的<student>標記
//qName表示讀取的標籤或標記名
//attributes 標記裏面的屬性集合,即標記間的內容
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tag=qName;//在開始賦值是爲了,內容判斷時判斷是什麼標記,下面的characters()要用來判斷
if("student".equals(tag)){
stu=new Student();//元素開始說明就要建立對象
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
//注意結束這裏比較的是結束傳進來的qName,而不是tagl了
if("student".equals(qName)){
list.add(stu);//結束時將對象添加到集合裏
}
tag="";//注意這裏結束一個標記時要賦空值,不然下面還有對象時沒法判斷元素
}
//標記與標記之間的內容
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String str=new String (ch,start,length);//獲得標記之間的內容,給Student對象賦值
//在開始標記tag,就是在這裏爲了判斷 
if("id".equals(tag)){
stu.id=Integer.parseInt(str);
}else if("name".equals(tag)){
stu.name=str;
}else if("age".equals(tag)){
stu.age=Integer.parseInt(str);
}
}
}
第二種:
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/*
 * XML中的內容,跟第一個不同的地方是,把id放在了<Student>標記裏面,根據xml創建javabean
 <?xml version="1.0" encoding="utf-8" ?>
<students>
<student id="1">
<name>張三</name>
<age>20</age>
</student>
<student id="2">
<name>李四</name>
<age>22</age>
</student>
<student id="3">
<name>王五</name>
<age>22</age>
</student>
</students>
 */
public class SaxHandle2 extends DefaultHandler{
    private List<Student> list;//用來存放數據
    Student stu;
    String tag;
    //返回一個集合供測試類調用
    public List<Student> getList(){
    return list;
    }
    //tag用來保存XML文檔的開始或結束標記名
@Override
public void startDocument() throws SAXException {
System.out.println("開始解析文檔,請稍後。。。。!");
list=new ArrayList<Student>();//開始就立刻建立集合
}
@Override
public void endDocument() throws SAXException {
System.out.println("文檔解析結束。。。。!!");
}
    //開始讀取文檔的標記,好比xml中自定義的<student>標記
//qName表示讀取的標籤或標記名
//attributes 標記裏面的屬性集合,即標記間的內容
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tag=qName;//在開始賦值是爲了,內容判斷時判斷是什麼標記characters()
if("student".equals(tag)){
stu=new Student();//元素開始說明就要建立對象
//id放在<student>裏面後要在這裏判斷,遍歷屬性
for(int i=0;i<attributes.getLength();i++){
String name=attributes.getQName(i);
String value=attributes.getValue(i);
if("id".equals(name)){
stu.id=Integer.parseInt(value);
}
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
//注意結束這裏比較的是結束傳進來的qName,而不是tagl了
if("student".equals(qName)){
list.add(stu);//結束時將對象添加到集合裏
}
tag="";//注意這裏結束一個標記時要賦空值,不然下面還有對象時沒法判斷元素
}
    //標記與標記之間的內容
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String str=new String(ch,start,length);//獲得標記之間的內容,給Student對象賦值
//在開始標記tag,就是在這裏爲了判斷
        if("name".equals(tag)){
stu.setName(str);
}else if("age".equals(tag)){
stu.age=Integer.parseInt(str);
}
}
}
測試類:
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class TestSaxHandle1 {
public static void main(String[] args) {
//獲得解析工廠
SAXParserFactory factory=SAXParserFactory.newInstance();
    try {
 //經過工廠獲得解析器
SAXParser parser = factory.newSAXParser();
SaxHandle1 handle1=new SaxHandle1();
//將文件和handler、解析器相關聯//文件路徑不能是中文,最好不要有中文
parser.parse("e:\\Student2.xml", handle1);
List<Student>list=handle1.getList();
//遍歷打印出來
for(Student s:list){
System.out.println(s);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相關文章
相關標籤/搜索