熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

使用Java自帶SAX工具解析XML

2022-06-13   來源: JSP教程 

  studentInfoxml


  

  <?xml version= encoding=gb?>
<student>
    <person age=><!如果沒有age屬性默認的為>
        <name>崔衛兵</name>
        <college>PC學院</college>
        <telephone></telephone>
        <notes>男年生碩士現就讀於北京郵電大學</notes>
    </person>
    <person>
        <name>cwb</name>
        <college leader=leader>PC學院</college><!如果沒有leader屬性

  認的為leader>
        <telephone></telephone>
        <notes>男年生碩士現就讀於中國農業大學</notes>
    </person>
    <person age=>
        <name>xxxxx</name>
        <college leader=學院領導>xxx學院</college>
        <telephone></telephone>
        <notes>注視中注釋中</notes>
    </person>
</student>

  SAXHandlerjava

  

  package saxExample;

  import javautilHashMap;
import orgxmlsax*;
import orgxmlsaxhelpersDefaultHandler;

  /**
 * 繼承DefaultHandler類用SAX實現對xml的遍歷
 * @author cuiweibing
 * @since
 */

  public class SAXHandler
    extends DefaultHandler {
  //存放所有的節點(這裡的節點等於原來的節點+編號)以及它所對應的值
  private HashMap<StringString> hashMap = new HashMap<StringString>();
  //目前的節點
  private String currentElement = null;
  //目前節點所對應的值
  private String currentValue = null;
  //用於節點編號(具體到person)
  private static int i=;
 
  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(qNameequalsIgnoreCase(student)){
      //currentElement= ;
    }else if (qNameequalsIgnoreCase(person)){
      i++;
      //currentElement= ;
      String age=attrgetValue(age);
      if(age!=null){
       hashMapput(qName+age+i age);
      }else{
       hashMapput(qName+age+i );
      }
    }else if (qNameequalsIgnoreCase(college)){
        currentElement= qName;
        String leader=attrgetValue(leader);
        if(leader!=null){
         hashMapput(qName+leader+i leader);
        }else{
         hashMapput(qName+leader+i leader);
        }
    }else{
      currentElement= qName;
    }
 
  }
 
  public void endElement(String uri String localName String qName) throws SAXException {
    if (qNameequalsIgnoreCase(student)){
//      hashMapput(currentElement currentValue);
    }else if (qNameequalsIgnoreCase(person)){
 
    }else{
      currentElement+=i;
      hashMapput(currentElement currentValue);
    }
  }

  TestSAXHandlerjava

  

  package saxExample;

  import javaioFile;
import javautilHashMap;
import javaxxmlparsersSAXParserFactory;
import javaxxmlparsersSAXParser;

  /**
 * 解析主類
 * @author cuiweibing
 * @since
 */

  public class TestSAXHandler {
  public TestSAXHandler() {
  }

  public static void main(String[] args) {
    try{
      //初始化與解析
      SAXHandler handler = new SAXHandler();
      SAXParserFactory saxparserfactory = SAXParserFactorynewInstance();
      SAXParser saxparser = saxparserfactorynewSAXParser();
      saxparserparse(new File(studentInfoxml) handler);

  //解析完後獲取解析信息
      HashMap hashMap = handlergetHashMap();
      Systemoutprintln(姓名\t年齡\t學院\t學院領導\t電話\t\t備注);
      for(int i=;i<hashMapsize();i+=){
        int j=i/;
        Systemoutprint(hashMapget(name+j)+\t);
        Systemoutprint(hashMapget(personage+j)+\t);
        Systemoutprint(hashMapget(college+j)+\t);
        Systemoutprint(hashMapget(collegeleader+j)+\t);
        Systemoutprint(hashMapget(telephone+j)+\t);
        Systemoutprintln(hashMapget(notes+j)+\t);
      }
    }catch(Exception ex){
      exprintStackTrace();
    }
  }
}


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19670.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.