SAX方式解析XML实例
XML文件:
SAX解析核心工厂调用代码
SAX解析核心代码
用的枚举类
调用测试
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
<contact id="1">
<name>zhangsan</name>
<sex>man</sex>
<mobile>13880246907</mobile>
<group>
<id>11</id>
<name>同事</name>
</group>
</contact>
<contact id="2">
<name>lisi</name>
<sex>man</sex>
<mobile>15828587260</mobile>
<group>
<id>21</id>
<name>家人</name>
</group>
</contact>
</contacts>
SAX解析核心工厂调用代码
package com.leiht.xml.sax4xml;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.leiht.xml.entity.Contact;
public class SaxParseXML {
public List<Contact> parse(String fileName) {
List<Contact> contacts = null;
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
ContactSaxHandler handler = new ContactSaxHandler();
parser.parse(fileName, handler);
contacts = handler.getResult();
} catch (Exception e) {
e.printStackTrace();
}
return contacts;
}
}
SAX解析核心代码
package com.leiht.xml.sax4xml;
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 com.leiht.xml.entity.Contact;
import com.leiht.xml.entity.Group;
public class ContactSaxHandler extends DefaultHandler {
private List<Contact> contacts = null;
private Contact contact = null;
private Group group = null;
private ContactEumState contactEumState = ContactEumState.none;
private boolean isGroupState = false;
public List<Contact> getResult() {
return contacts;
}
@Override
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startDocument()
* 解析文档开始 接收文档开始的通知
*/
public void startDocument() throws SAXException {
contacts = new ArrayList<Contact>();
}
@Override
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
* @接收元素开始的通知 即开始读XML文档的标签
*/
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//读取contact节点,读ID
if(qName.equals("contact")) {
contact = new Contact();
contact.setId(Integer.parseInt(attributes.getValue("id")));
return;
}
if(qName.equals("name")) {
if(!isGroupState) {
contactEumState = ContactEumState.contactName;
}
if(isGroupState) {
contactEumState = ContactEumState.groupName;
}
return;
}
if(qName.equals("sex")) {
contactEumState = ContactEumState.sex;
return;
}
if(qName.equals("mobile")) {
contactEumState = ContactEumState.mobile;
return;
}
if(qName.equals("group")) {
group = new Group();
isGroupState = true;
return;
}
if(qName.equals("id")) {
contactEumState = ContactEumState.groupId;
return;
}
}
@Override
/*
* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
* 读每个TAG之间的内容
*/
public void characters(char[] ch, int start, int length)
throws SAXException {
String result = new String(ch, start,length);
switch (contactEumState){
case contactName :
contact.setName(result);
contactEumState = ContactEumState.none;
break;
case mobile :
contact.setMobile(result);
contactEumState = ContactEumState.none;
break;
case sex :
contact.setSex(result);
contactEumState = ContactEumState.none;
break;
case groupId :
group.setId(result);
contactEumState = ContactEumState.none;
break;
case groupName :
group.setName(result);
contactEumState = ContactEumState.none;
break;
default: break;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals("group")) {
isGroupState = false;
contact.setGroup(group);
group = null;
return;
}
if(qName.equals("contact")) {
contacts.add(contact);
contact = null;
return;
}
}
@Override
public void endDocument() throws SAXException {
System.out.println("解析完成");
}
}
用的枚举类
package com.leiht.xml.sax4xml;
public enum ContactEumState {
contactName,mobile,sex,groupId,groupName,none
}
调用测试
package com.leiht.xml.view;
import java.util.List;
import com.leiht.xml.dom4xml.DomParseXML;
import com.leiht.xml.entity.Contact;
import com.leiht.xml.sax4xml.SaxParseXML;
public class Test {
public static void main(String[] args) {
//DomParseXML domParseXML = new DomParseXML();
//List<Contact> contacts = domParseXML.parse("xml/contact.xml");
SaxParseXML samParseXML = new SaxParseXML();
List<Contact> contacts = samParseXML.parse("xml/contact.xml");
for(Contact contact : contacts){
System.out.println(contact);
}
}
}