1.sax
流解析,character函数可能执行多次,数据被截断,用StringBuffer在character方法中拼接字符串,在end方法中取数据才不会断,遇到一个问题,之前这么写的,在windows执行没有问题,但换到linux后有时候character被多次执行
/** * */ package com.jiudao.www.util; import java.io.InputStream; 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; /** * @author lenovo0- * */ public class SaxParseService extends DefaultHandler{ private List<String> elementsList = null; private String datalogelement = null; private String preTag = null;//作用是记录解析时的上一个节点名称 public List<String> getElements(InputStream xmlStream) throws Exception{ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SaxParseService handler = new SaxParseService(); parser.parse(xmlStream, handler); return handler.getDatalogElements(); } public List<String> getDatalogElements(){ return elementsList; } @Override public void startDocument() throws SAXException { elementsList = new ArrayList<String>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { preTag = qName;//将正在解析的节点名称赋给preTag } @Override public void endElement(String uri, String localName, String qName) throws SAXException { 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(preTag.equals("datalogelement")){ elementsList.add(content); //System.out.println("content:"+content); } } // } }
修改后的代码
/** * */ package com.jiudao.www.util; import java.io.InputStream; 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; /** * @author lenovo0- * */ public class SaxParseService extends DefaultHandler{ private List<String> elementsList = null; private String preTag = null;//作用是记录解析时的上一个节点名称 private StringBuilder sb; public List<String> getElements(InputStream xmlStream) throws Exception{ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SaxParseService handler = new SaxParseService(); parser.parse(xmlStream, handler); return handler.getDatalogElements(); } public List<String> getDatalogElements(){ return elementsList; } @Override public void startDocument() throws SAXException { elementsList = new ArrayList<String>(); sb = new StringBuilder(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { preTag = qName;//将正在解析的节点名称赋给preTag } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if(qName.equals("datalogelement")){ elementsList.add(sb.toString()); } 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(preTag.equals("datalogelement")){ // elementsList.add(content); // //System.out.println("content:"+content); // } sb.append(content); } } }
2.dom
把文件加载到内存中,不适用于大文件