欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-21 13:36:46
复制代码 代码如下:package com.test; import java.io.file;import java.io.fileinputstream;import...

复制代码 代码如下:

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);
        }
    }

}