国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 15:48:31
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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 當讀到一個開始標簽的時候,會觸發這個方法
        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 當遇到文檔的開頭的時候,調用這個方法
        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 這個方法用來處理在XML文件中讀到的內容
        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 在遇到結束標簽的時候,調用這個方法
        super.endElement(uri, localName, name);

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

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东至县| 定远县| 鄯善县| 商南县| 禹州市| 建阳市| 芮城县| 明光市| 玛纳斯县| 汾西县| 北票市| 建德市| 胶南市| 信宜市| 禹城市| 新余市| 宁武县| 于田县| 鱼台县| 望江县| 乌什县| 志丹县| 漳平市| 九寨沟县| 湖南省| 大厂| 绥棱县| 仙桃市| 绥德县| 师宗县| 南澳县| 临海市| 星座| 河北省| 琼中| 祁阳县| 林芝县| 绥滨县| 富民县| 秭归县| 洞口县|