分类:
2008-09-09 16:49:15
package saxExample;
import java.util.HashMap;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
/**
* 继承DefaultHandler类,用SAX实现对xml的遍历
* @author cuiweibing
* @since 2007.8.8
*/
public class SAXHandler
extends DefaultHandler {
//存放所有的节点(这里的节点等于原来的节点+编号)以及它所对应的值
private HashMap
//目前的节点
private String currentElement = null;
//目前节点所对应的值
private String currentValue = null;
//用于节点编号(具体到person)
private static int i=-1;
public HashMap getHashMap() {
return hashMap;
}
public void characters(char[] ch, int start, int length) throws SAXException {
//取出目前节点对应的值
currentValue = new String(ch, start, length);
}
public void startElement(String uri, String localName, String qName,
Attributes attr) throws SAXException {
if(qName.equalsIgnoreCase("student")){
//currentElement= "";
}else if (qName.equalsIgnoreCase("person")){
i++;
//currentElement= "";
String age=attr.getValue("age");
if(age!=null){
hashMap.put(qName+"-age"+i, age);
}else{
hashMap.put(qName+"-age"+i, "20");
}
}else if (qName.equalsIgnoreCase("college")){
currentElement= qName;
String leader=attr.getValue("leader");
if(leader!=null){
hashMap.put(qName+"-leader"+i, leader);
}else{
hashMap.put(qName+"-leader"+i, "leader");
}
}else{
currentElement= qName;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("student")){
// hashMap.put(currentElement, currentValue);
}else if (qName.equalsIgnoreCase("person")){
}else{
currentElement+=i;
hashMap.put(currentElement, currentValue);
}
}
}
package saxExample;
import java.io.File;
import java.util.HashMap;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
/**
* 解析主类
* @author cuiweibing
* @since 2007.8.8
*/
public class TestSAXHandler {
public TestSAXHandler() {
}
public static void main(String[] args) {
try{
//初始化与解析
SAXHandler handler = new SAXHandler();
SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
SAXParser saxparser = saxparserfactory.newSAXParser();
saxparser.parse(new File("studentInfo.xml"), handler);
//解析完后获取解析信息
HashMap hashMap = handler.getHashMap();
System.out.println("姓名\t年龄\t学院\t学院领导\t电话\t\t备注");
for(int i=0;i
System.out.print(hashMap.get("name"+j)+"\t");
System.out.print(hashMap.get("person-age"+j)+"\t");
System.out.print(hashMap.get("college"+j)+"\t");
System.out.print(hashMap.get("college-leader"+j)+"\t");
System.out.print(hashMap.get("telephone"+j)+"\t");
System.out.println(hashMap.get("notes"+j)+"\t");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}