使用Java自带SAX工具解析XML

| 1 Comment | No TrackBacks

studentInfo.xml

<?xml version="1.0" encoding="gb2312"?>
<student>
    <person age="25"><!--如果没有age属性,默认的为20-->
        <name>崔卫兵</name>
        <college>PC学院</college>
        <telephone>62354666</telephone>
        <notes>男,1982年生,硕士,现就读于北京邮电大学</notes>
    </person>
    <person>
        <name>cwb</name>
        <college leader="leader1">PC学院</college><!--如果没有leader属性,默

认的为leader-->
        <telephone>62358888</telephone>
        <notes>男,1987年生,硕士,现就读于中国农业大学</notes>
    </person>
    <person age="45">
        <name>xxxxx</name>
        <college leader="学院领导">xxx学院</college>
        <telephone>66666666</telephone>
        <notes>注视中,注释中</notes>
    </person>
</student>

 

SAXHandler.java

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<String,String> hashMap = new HashMap<String,String>();
  //目前的节点
  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);
    }
  }
}

TestSAXHandler.java

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<hashMap.size();i+=6){
        int j=i/6;
        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();
    }
  }
}

No TrackBacks

TrackBack URL: http://www.wujianrong.com/mt-tb.cgi/2997

1 Comment

这几天也做了这个东西,交换链接ok?www.javaflag.com,你的已加,做好留言!谢谢!

Leave a comment

About this Entry

This page contains a single entry by kevinwu published on August 14, 2007 1:10 AM.

Java开发框架调查:6%的JSF欲撼动21%的Struts was the previous entry in this blog.

JSP中include指令和include行为的区别 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.