SAX解析XML

使用简单方式解析:
package com.rui.sax;

import java.util.Stack;

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 SaxTest2 {
	
	public static void main(String[] args) throws Exception 
	{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		
		SAXParser parser = factory.newSAXParser();
		
		parser.parse("student.xml", new MyHandler2());
	}

}


class MyHandler2 extends DefaultHandler {
	
	private Stack<String> stack = new Stack<String>();
	
	private String name;
	private String gender;
	private String age;
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		stack.push(qName);
		
		for(int i=0;i<attributes.getLength();i++){
			String attrName = attributes.getQName(i);
			String attrValue = attributes.getValue(i);
			
			System.out.println(attrName + "=" + attrValue);
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String tag = stack.peek();
		
		if("姓名".equals(tag)) {
			name = new String(ch,start,length);
		}else if("性别".equals(tag)){
			gender = new String(ch,start,length);
		}else if("年龄".equals(tag)){
			age = new String(ch,start,length);
		}
	}

	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		stack.pop(); // 表示该元素已经解析完毕,需要从栈中弹出 
		
		if("学生".equals(qName)) {
			System.out.println("姓名:" + name);
			System.out.println("性别:" + gender);
			System.out.println("年龄:" + age); 
		}
		
	}
}

学生类:
package com.rui.sax;

public class Student {

	private String name;
	private String gender;
	private String age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public Student() {
	}

	public Student(String name, String gender, String age) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
	}

}



XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<学生名册 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Course30\student.xsd">
	<学生 学号="1">
		<name>张三</name>
		<gender>男</gender>
		<age>20</age>
	</学生>
	<学生 学号="2">
		<name>李四</name>
		<gender>女</gender>
		<age>19</age>
	</学生>
	<学生 学号="3">
		<name>王五</name>
		<gender>男</gender>
		<age>22</age>
	</学生>
</学生名册>


SAX生成XML文件:
package com.rui.sax;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.xml.sax.ContentHandler;
import org.xml.sax.helpers.AttributesImpl;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class TestSAXWriter {

    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream(new File("students.xml"));
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        
        OutputFormat out = new OutputFormat("XML", "UTF-8", true);
        out.setIndent(1);
        out.setIndenting(true);
        
        XMLSerializer serializer = new XMLSerializer(bos, out);
        
        ContentHandler hd = serializer.asContentHandler();
        hd.startDocument();
        
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "age", "", "10");
        
        String name = "张三";
        
        hd.startElement("", "", "students",null);
        
        hd.startElement("", "", "student", atts);
        hd.characters(name.toCharArray(), 0, name.length());
        hd.endElement("", "", "student");
        
        name = "李四";
        
        hd.startElement("", "", "student", null);
        hd.characters(name.toCharArray(), 0, name.length());
        hd.endElement("", "", "student");
        
        hd.endElement("", "", "students");

        hd.endDocument();
        
        fos.close();
        bos.close();
    }

}

项目代码如附件:

你可能感兴趣的:(解析xml)