欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java解析xml之sax解析xml示例分享

 更新時(shí)間:2014年01月05日 09:50:27   作者:  
SAX基于事件的解析,解析器在一次讀取XML文件中根據(jù)讀取的數(shù)據(jù)產(chǎn)生相應(yīng)的事件,由應(yīng)用程序?qū)崿F(xiàn)相應(yīng)的事件處理邏輯,即它是一種“推”的解析方式;這種解析方法速度快、占用內(nèi)存少,但是它需要應(yīng)用程序自己處理解析器的狀態(tài),實(shí)現(xiàn)起來會比較麻煩

復(fù)制代碼 代碼如下:

package com.test;

import java.io.File;
import java.io.FileInputStream;
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;

public class SaxXML {

    public static void main(String[] args) {
        File file = new File("e:/People.xml");
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = spf.newSAXParser();
            SaxHandler handler = new SaxHandler("People");
            parser.parse(new FileInputStream(file), handler);

            List<People> peopleList = handler.getPeoples();
            for(People people : peopleList){
                System.out.println(people.getId()+"\t"+people.getName()+"\t"+people.getEnglishName()+"\t"+people.getAge());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class SaxHandler extends DefaultHandler {
    private List<People> peoples = null;
    private People people;
    private String currentTag = null;
    private String currentValue = null;
    private String nodeName = null;

    public List<People> getPeoples() {
        return peoples;
    }

    public SaxHandler(String nodeName) {
        this.nodeName = nodeName;
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO 當(dāng)讀到一個(gè)開始標(biāo)簽的時(shí)候,會觸發(fā)這個(gè)方法
        super.startDocument();

        peoples = new ArrayList<People>();
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO 自動生成的方法存根
        super.endDocument();
    }

    @Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        // TODO 當(dāng)遇到文檔的開頭的時(shí)候,調(diào)用這個(gè)方法
        super.startElement(uri, localName, name, attributes);

        if (name.equals(nodeName)) {
            people = new People();
        }
        if (attributes != null && people != null) {
            for (int i = 0; i < attributes.getLength(); i++) {
                if(attributes.getQName(i).equals("id")){
                    people.setId(attributes.getValue(i));
                }
                else if(attributes.getQName(i).equals("en")){
                    people.setEnglishName(attributes.getValue(i));
                }
            }
        }
        currentTag = name;
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO 這個(gè)方法用來處理在XML文件中讀到的內(nèi)容
        super.characters(ch, start, length);

        if (currentTag != null && people != null) {
            currentValue = new String(ch, start, length);
            if (currentValue != null && !currentValue.trim().equals("") && !currentValue.trim().equals("\n")) {
                if(currentTag.equals("Name")){
                    people.setName(currentValue);
                }
                else if(currentTag.equals("Age")){
                    people.setAge(currentValue);
                }
            }
        }
        currentTag = null;
        currentValue = null;
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        // TODO 在遇到結(jié)束標(biāo)簽的時(shí)候,調(diào)用這個(gè)方法
        super.endElement(uri, localName, name);

        if (name.equals(nodeName)) {
            peoples.add(people);
        }
    }

}

相關(guān)文章

  • 淺談Java中復(fù)制數(shù)組的方式

    淺談Java中復(fù)制數(shù)組的方式

    這篇文章主要介紹了Java中復(fù)制數(shù)組的幾種方法,需要的朋友可以參考下。
    2017-08-08
  • 詳解SpringBoot基礎(chǔ)之banner玩法解析

    詳解SpringBoot基礎(chǔ)之banner玩法解析

    SpringBoot項(xiàng)目啟動時(shí)會在控制臺打印一個(gè)默認(rèn)的啟動圖案,這個(gè)圖案就是我們要講的banner,這篇文章主要介紹了SpringBoot基礎(chǔ)之banner玩法解析,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 五分鐘帶你學(xué)會用java解析json字符串

    五分鐘帶你學(xué)會用java解析json字符串

    這篇文章主要給大家介紹了關(guān)于用java解析json字符串的相關(guān)資料,JSON是一種輕量級的、基于文本的、與語言無關(guān)的數(shù)據(jù)交換格式,易于人和機(jī)器讀寫,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • 淺談SpringMVC國際化支持

    淺談SpringMVC國際化支持

    這篇文章主要介紹了淺談SpringMVC國際化支持,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • C++ 虛函數(shù)與純虛函數(shù)代碼詳解

    C++ 虛函數(shù)與純虛函數(shù)代碼詳解

    本文主要介紹了C++ 虛函數(shù)與純虛函數(shù)的使用與區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Mybatis常用標(biāo)簽及屬性小結(jié)

    Mybatis常用標(biāo)簽及屬性小結(jié)

    這篇文章主要介紹了Mybatis常用標(biāo)簽及屬性小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-12-12
  • Java單例模式、饑餓模式代碼實(shí)例

    Java單例模式、饑餓模式代碼實(shí)例

    這篇文章主要介紹了Java單例模式、饑餓模式代碼實(shí)例,本文直接給出代碼實(shí)例,需要的朋友可以參考下
    2015-05-05
  • ElasticSearch學(xué)習(xí)之Es索引Api操作

    ElasticSearch學(xué)習(xí)之Es索引Api操作

    這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之Es索引Api操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • IDEA新建Springboot項(xiàng)目(圖文教程)

    IDEA新建Springboot項(xiàng)目(圖文教程)

    下面小編就為大家?guī)硪黄狪DEA新建Springboot項(xiàng)目(圖文教程)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • SpringBoot項(xiàng)目使用協(xié)同過濾的實(shí)現(xiàn)

    SpringBoot項(xiàng)目使用協(xié)同過濾的實(shí)現(xiàn)

    協(xié)同過濾是一種常用的推薦系統(tǒng)算法,用于預(yù)測用戶可能喜歡的物品,本文主要介紹了SpringBoot項(xiàng)目使用協(xié)同過濾的實(shí)現(xiàn),感興趣的可以了解一下
    2023-09-09

最新評論