<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    甜咖啡

    我的IT空間

    SAX解析XML

    package com;
      
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.xml.parsers.SAXParser;
      import javax.xml.parsers.SAXParserFactory;
      
      import org.xml.sax.Attributes;
      import org.xml.sax.SAXException;
      import org.xml.sax.helpers.DefaultHandler;
      
      /**
       * SAX解析XML,事件驅(qū)動(dòng)
       * 只有兩種節(jié)點(diǎn)
       * Element Node元素節(jié)點(diǎn)
       * Text Node文本節(jié)點(diǎn) 
       */
      public class SaxResolveXML {
     
      public static void main(String[] args){
      try {
      SaxResolveXML saxResolveXML = new SaxResolveXML();
      InputStream inStream = new FileInputStream("D:\\xml.xml");
      List<Person> list = saxResolveXML.getList(inStream);
      for(Person person : list){
      System.out.println(person.toString());
      }
      } catch (Exception e) {
      e.printStackTrace();
      }
     
      }
     
      public List<Person> getList(InputStream inStream) throws Exception {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser parse = factory.newSAXParser();
      SaxResolve saxResolve = new SaxResolve();
      parse.parse(inStream, saxResolve);
      inStream.close();
      return saxResolve.getPerson();
      }
     
      private final class SaxResolve extends DefaultHandler {
     
      private List<Person> list = null;
      private Person person = null;
      private String tag = null;
     
      public List<Person> getPerson(){
      return list;
      }
     
      //開始文檔事件
      public void startDocument() throws SAXException {
      //初始化
      list = new ArrayList<Person>();
      }
      
      //開始元素語(yǔ)法事件  參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名、屬性
      public void startElement(String uri, String localName, String qName,
      Attributes atts) throws SAXException {
      if("person".equals(qName)){
      person = new Person();
      person.setId(Integer.parseInt(atts.getValue(0)));
      }
      tag = qName;
      }
     
      //觸發(fā)文本節(jié)點(diǎn)事件  參數(shù)說(shuō)明:整個(gè)xml內(nèi)容的字符串、當(dāng)前讀到文本類型的開始位置、當(dāng)前讀到文本的數(shù)據(jù)長(zhǎng)度
      public void characters(char[] ch, int start, int length)
      throws SAXException {
      if(tag != null){
      String data = new String(ch, start, length);
      if(tag.equals("name")){
      person.setName(data);
      }else if(tag.equals("age")){
      person.setAge(Integer.parseInt(data));
      }
      }
      }
     
      //結(jié)束元素語(yǔ)法事件  參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名
      public void endElement(String uri, String localName, String qName)
      throws SAXException {
      if("person".equals(qName)){
      list.add(person);
      person = null;
      //對(duì)象設(shè)為空
      }
      tag = null;
      }
      }
     
     
      /*//開始文檔事件
      public void startDocument() throws SAXException {
     
      }
      
      //開始元素語(yǔ)法事件  參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名、屬性
      public void startElement(String uri, String localName, String qName,
      Attributes atts) throws SAXException {
     
      }
     
      //觸發(fā)文本節(jié)點(diǎn)事件  參數(shù)說(shuō)明:整個(gè)xml內(nèi)容的字符串、當(dāng)前讀到文本類型的開始位置、當(dāng)前讀到文本的數(shù)據(jù)長(zhǎng)度
      public void characters(char[] ch, int start, int length)
      throws SAXException {
     
      }
     
      //結(jié)束元素語(yǔ)法事件  參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名
      public void endElement(String uri, String localName, String qName)
      throws SAXException {
     
      }
      
      public void endDocument() throws SAXException {
     
      }
      
      public void endPrefixMapping(String prefix) throws SAXException {
     
      }
      
      public void ignorableWhitespace(char[] ch, int start, int length)
      throws SAXException {
     
      }
      
      public void processingInstruction(String target, String data)
      throws SAXException {
     
      }
      
      public void setDocumentLocator(Locator locator) {
     
      }
      
      public void skippedEntity(String name) throws SAXException {
     
      }
      
      public void startPrefixMapping(String prefix, String uri)
      throws SAXException {
     
      }*/
     
      }
      
     
     
     
     xml文件如下:
     <?xml version="1.0" encoding="UTF-8"?>
      <persons>
          <person id="1">
              <name>liming</name>
              <age>23</age>
          </person>
          <person id="2">
              <name>lixiangmei</name>
              <age>24</age>
          </person>
      </persons>
      
     
     
     

    posted on 2012-12-25 16:52 甜咖啡 閱讀(376) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    <2012年12月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    統(tǒng)計(jì)

    常用鏈接

    留言簿(1)

    我參與的團(tuán)隊(duì)

    隨筆檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 中文字幕亚洲激情| 国产大片91精品免费观看不卡| 精品无码国产污污污免费网站 | 亚洲av无码无在线观看红杏| 亚洲精品伦理熟女国产一区二区| 99视频免费播放| 久久精品国产亚洲AV麻豆王友容| 亚洲乱码卡一卡二卡三| 中文成人久久久久影院免费观看| 一个人看www在线高清免费看| 亚洲黄色三级视频| 美女被免费网站91色| 黄页网站免费观看| 亚洲精品中文字幕乱码影院| 免费A级毛片无码A∨中文字幕下载| 精品亚洲一区二区三区在线播放| 老司机午夜在线视频免费| 国产成人青青热久免费精品| 日韩电影免费在线观看网址| 中文字幕亚洲一区| 久草视频在线免费看| 亚洲春色在线观看| 国产精品麻豆免费版| 一级做a爰片久久毛片免费陪| 亚洲国产成人高清在线观看| 亚洲黄色免费网址| 国产午夜亚洲精品不卡免下载| 久久久久亚洲精品无码网址| 最近新韩国日本免费观看| 亚洲色偷偷综合亚洲av78| 亚洲AⅤ永久无码精品AA| 免费毛片a线观看| 亚洲成a人片在线观看精品| 四虎永久免费地址在线网站 | 99精品全国免费观看视频..| 亚洲精品中文字幕乱码影院 | 国产亚洲精午夜久久久久久| 中文字幕免费在线观看| 亚洲狠狠色丁香婷婷综合| 久久99亚洲综合精品首页| 3344免费播放观看视频|