通常解析xml文件我们主要用到的时候利用第三方的jar包如:jdom,dom4j来解析xml文件,但是这里使用的是jdk自带的解析类库,使用jdk自带的类库SAX方式解析的话,解析效率会比较高:
当我们使用jdk自带的解析类库的话,我们必须要继承DefaultHandler.java的类库:
xml事件处理类:
/**
*
*/
package com.sandy.xml.util;
import java.util.HashMap;
import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author sandy
* @since
*
*/
public class XMLHandler extends DefaultHandler {
private Map<String, Object> props = null;
private StringBuffer currentValue = null;
public XMLHandler() {
this.props = new HashMap<String, Object>();
this.currentValue = new StringBuffer();
}
/**
* @return the props
*/
public Map<String, Object> getProps() {
return props;
}
/**
* @param props
* the props to set
*/
public void setProps(Map<String, Object> props) {
this.props = props;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
int length=attributes.getLength();
if(length!=0){
for(int i=0;i<length;i++){
System.out.println(attributes.getQName(i)+"="+attributes.getValue(i));
}
}
currentValue.delete(0, currentValue.length());
System.out.println("(startElement:qName)==="+qName);
// this.currentName = qName;
}
/**
* @author sandy
* 获取标签内容
*/
public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue.append(ch, start, length);
System.out.println("characters+currentValue:" + currentValue);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (!qName.equalsIgnoreCase("root"))
props.put(qName, currentValue.toString().trim());
System.out.println("(endElement:qName)==="+qName);
}
}
解析类:
/**
*
*/
package com.sandy.xml.parse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import com.sandy.xml.util.XMLHandler;
/**
* @author sandy
*
*/
public class ParseXML {
private Map<String, Object> props=null;
private void parse(InputStream in) throws IOException {
XMLHandler handler = new XMLHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = null;
try {
try {
parser = factory.newSAXParser();
parser.parse(in, handler);
props = handler.getProps();
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException");
e.printStackTrace();
} catch (SAXException e) {
System.out.println("SAXException");
e.printStackTrace();
}
} finally {
factory = null;
parser = null;
handler = null;
}
}
/**
* @return the props
*/
public Map<String, Object> getProps() {
return props;
}
public static void main(String[] args) {
ParseXML xml = new ParseXML();
String xmlString="<ROOT type='request'>"
+"<stdmsgtype>0100</stdmsgtype>"
+"<std400trcd>201101</std400trcd>"
+"<stdprocode>201101</stdprocode>"
+"<std400aqid>N</std400aqid>"
+"<std400tlno>0279</std400tlno>"
+"<stdlocdate>20100423</stdlocdate>"
+"<stdloctime>110541</stdloctime>"
+"<std400autl></std400autl>"
+"<stdauthid></stdauthid>"
+"<stdadddtap></stdadddtap>"
+"<stdibsdate></stdibsdate>"
+"<stdrefnum></stdrefnum>"
+"<stdsetdate></stdsetdate>"
+"<std400trno></std400trno>"
+"<std400mgid></std400mgid>"
+"<std400acur></std400acur>"
+"<stdmercno>010100099970014</stdmercno>"
+"<M_CODE>000000000000000025</M_CODE>"
+"<M_CODETYPE>0</M_CODETYPE>"
+"</ROOT>";
try {
xml.parse(new ByteArrayInputStream(xmlString.getBytes()));
Map<String,Object> m=xml.getProps();
Set<Map.Entry<String, Object>> s=m.entrySet();
for(Object o:s){
System.out.println(o);
}
} catch (IOException e) {
System.out.println("解析xml出错!");
e.printStackTrace();
}
}
}
说明:这种解析方式是基于事件驱动的,运用的是基于观察者的设计模式.