(一)SAX解析
1.1 SAX解析
SAX:基于事件处理的机制
sax解析xml文件时,遇到根开始标签,根结束标签,开始解析文件,文件解析结束,字符内容,空白字符等都会触发各自的方法。
优点:
适合解析大文件,对内存要求不高
轻量级的解析数据方式,效率更高
缺点:
不能随机解析(只能从头开始解析,一点点往后进行)
不能修改XML文件,只能进行查询
1.2用SAX解析遍历整个xml文件并进行原样输出
1.2.1 java代码
package xml4; import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; 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 SaxTest { public static void main(String[] args) { // SAX解析 ①获取factory工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); try { // SAX ②获取解析器 SAXParser parser = factory.newSAXParser(); // 获取handel的对象 SaxTestHandel sh = new SaxTestHandel(); try { parser.parse(new File("src/xml4/desk.xml"), sh); } catch (IOException e) { e.printStackTrace(); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } } } class SaxTestHandel extends DefaultHandler { //当开始读到这个文件时,触发该相应 @Override public void startDocument() throws SAXException { System.out.println("开始解析了!"); } //读完了,所有的,触发该方法 @Override public void endDocument() throws SAXException { //System.out.println("解析完了!"); } //读到元素标签时,触发该方法 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.print("<"); System.out.print(qName); for(int i=0;i<attributes.getLength();i++) { String qName2 = attributes.getQName(i); String value = attributes.getValue(qName2); System.out.print(" "+qName2+"="+"'"+value+"'"); } System.out.print(">"); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.print("</"+qName+">"); } @Override public void characters(char[] ch, int start, int length) throws SAXException { String str=new String(ch,start,length); System.out.print(str); } }
1.2.2 Xml代码
<?xml version="1.0" encoding="UTF-8"?> <disks> <disk name='c盘'> <size>10G</size> <directory>100</directory> <file>200</file> </disk> <disk name='d盘'> <size>20G</size> <directory>200</directory> <file>3000</file> </disk> </disks>
1.3 小练习 题目:获取一个xml 文件中的信息,并把它封装到一个实体类中去,并存放到一个集合中去,并保存到本地文件中去
1 package xml4; 2 3 public class Disk { 4 5 private String name; 6 private String size; 7 private String directory; 8 private int file; 9 public Disk(String name, String size, String directory, int file) { 10 super(); 11 this.name = name; 12 this.size = size; 13 this.directory = directory; 14 this.file = file; 15 } 16 public Disk() { 17 super(); 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public String getSize() { 26 return size; 27 } 28 public void setSize(String size) { 29 this.size = size; 30 } 31 public String getDirectory() { 32 return directory; 33 } 34 public void setDirectory(String directory) { 35 this.directory = directory; 36 } 37 public int getFile() { 38 return file; 39 } 40 public void setFile(int file) { 41 this.file = file; 42 } 43 @Override 44 public String toString() { 45 return "Disk [name=" + name + ", size=" + size + ", directory=" + directory + ", file=" + file + "]"; 46 } 47 48 }
package xml4; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.xml.parsers.ParserConfigurationException; 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 sun.awt.FwDispatcher; public class SaxTest2 { public static void main(String[] args) { SAXParserFactory factory = SAXParserFactory.newInstance(); try { File f = new File("src/xml4/desk.xml"); SaxTest2Hander dh = new SaxTest2Hander(); SAXParser parser = factory.newSAXParser(); try { parser.parse(f, dh); } catch (IOException e) { e.printStackTrace(); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } } } class SaxTest2Hander extends DefaultHandler { private ArrayList<Disk> list; private Disk disk; private String tagName; @Override public void startDocument() throws SAXException { list=new ArrayList<Disk>(); } @Override public void endDocument() throws SAXException { try { PrintWriter fw=new PrintWriter("src/xml4/desk.txt"); for (Disk disk : list) { fw.println(disk); fw.flush(); } fw.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("disk".equals(qName)) { disk=new Disk(); }else { tagName=qName; } for(int i=0;i<attributes.getLength();i++) { String qName2 = attributes.getQName(0); String value = attributes.getValue(qName2); if(qName2.equals("name")){ disk.setName(value); } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if("disk".equals(qName)) { list.add(disk); }else { tagName=null; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { String str=new String(ch,start,length); if("size".equals(tagName)) { disk.setSize(str); } if("directory".equals(tagName)) { disk.setDirectory(str); } if("file".equals(tagName)) { disk.setFile(Integer.parseInt(str)); } } }
<?xml version="1.0" encoding="UTF-8"?> <disks> <disk name='c盘'> <size>10G</size> <directory>100</directory> <file>200</file> </disk> <disk name='d盘'> <size>20G</size> <directory>200</directory> <file>3000</file> </disk> </disks>