XMLはSAX方式で解析する
6196 ワード
詳細
1.XMLはSAX方式で解析
1.1 SAX方式でXMLファイルを読み込む
1.2 SAX方式でXMLデータをBeanに読み出す
1.XMLはSAX方式で解析
SAX , (document) 、 (element) 、 (document) , , , 。
: ; ;
: ; ;
1.1 SAX方式でXMLファイルを読み込む
20
21
package com.andrew.xml;
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 SAX01 extends DefaultHandler {
@Override
public void startDocument() throws SAXException {
System.out.println("");
}
@Override
public void endDocument() throws SAXException {
System.out.print("
");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.print("");
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.print("");
System.out.print(qName);
System.out.print(">");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
System.out.print(new String(ch, start, length));
}
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse("src/students.xml", new SAX01());
}
}
:
20
21
1.2 SAX方式でXMLデータをBeanに読み出す
20
21
package com.andrew.model;
public class Student {
private String id;
private String name;
private String sex;
private int age;
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
// getter and setter
}
package com.andrew.xml;
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;
import com.andrew.model.Student;
public class SAX02 extends DefaultHandler {
private List students = null;
private Student student = null;
private String preTag = null; //
@Override
public void startDocument() throws SAXException {
System.out.println(" ");
students = new ArrayList();
}
@Override
public void endDocument() throws SAXException {
System.out.println(" ");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("student".equals(qName)) {
student = new Student();
student.setId(attributes.getValue(0));
}
preTag = qName;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("student".equals(qName)) {
students.add(student);
student = null;
}
preTag = null;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (preTag != null) {
String content = new String(ch, start, length);
if ("name".equals(preTag)) {
student.setName(content);
} else if ("sex".equals(preTag)) {
student.setSex(content);
} else if ("age".equals(preTag)) {
student.setAge(Integer.parseInt(content));
}
}
}
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
SAX02 sax02 = new SAX02();
parser.parse("src/studentsSax.xml", sax02);
for (Student s : sax02.students) {
System.out.println(s);
}
}
}
:
Student [id=001, name= , sex= , age=20]
Student [id=002, name= , sex= , age=21]