标签:actor 文件 star 结束 else class length java 优点
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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); } @Override public void endDocument() throws SAXException { //结束扫描文档事件 System.out.print("\n 扫描文档结束"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { //开始扫描元素事件 uri前缀 System.out.print("<"); System.out.print(qName); if(attributes!=null){ for(int i=0;i<attributes.getLength();i++){ System.out.print(" "+attributes.getQName(i)+"=\""+attributes.getValue(i)+"\""); } } 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/student.xml", new SAX01()); } }
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.learn.model.Student; public class SAX02 extends DefaultHandler{ private List<Student> students=null; private Student student=null; private String preTag=null; // 标记上一个节点名称 @Override public void startDocument() throws SAXException { System.out.println("开始读取学生信息"); students=new ArrayList<Student>(); } @Override public void endDocument() throws SAXException { System.out.println("\n 学生信息读取完毕"); } @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/students.xml", sax02); for(Student s:sax02.students){ System.out.println(s); } } }
标签:actor 文件 star 结束 else class length java 优点
原文地址:https://www.cnblogs.com/Cocoomg/p/9901615.html