編程用JAVA解析XML的方式

<?xml version="1.0" encoding="gb2312"?>
<person>
  <name>
王小明</name>
  <college>
信息學院</college>  
  <telephone>6258113</telephone>
  <notes>
,1955年生,博士,95年調入海南大學</notes>
</person>

事件回調類SAXHandler.java

import java.io.*;
import java.util.Hashtable;
import org.xml.sax.*;

public class SAXHandler extends HandlerBase {
  private Hashtable table = new Hashtable();
  private String currentElement = null;
  private String currentValue = null;

  public void setTable(Hashtable table) {
    this.table = table;
  }

  public Hashtable getTable() {
    return table;
 }

  public void startElement(String tag, AttributeList attrs) throws SAXException {
    currentElement = tag;
  }

  public void characters(char[] ch, int start, int length) throws SAXException {
    currentValue = new String(ch, start, length);
  }

  public void endElement(String name) throws SAXException {
    if (currentElement.equals(name)) {
      table.put(currentElement, currentValue);
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章