H2通用缓存加载(2)——使用sax解析xml

来源:互联网 发布:手机维修哪家好 知乎 编辑:程序博客网 时间:2024/06/07 15:53

H2LoadConfig.xml

 

<?xml version="1.0" encoding="gb2312"?><h2>    <table name="zgcode" text="职工代码表" database="zg" loadClass="com.dyna.report.config.H2load.ZgCodeLoadCache">    <columns><column key="aaa102"  text="代码值" type="varchar(6)" order="0"></column><column key="aaa103" text="代码名称" type="varchar(100)" order="1"></column><column key="aaa100" text="代码" type="varchar(20)" order="2"></column><column key="aaz093" text="代码表id" type="long" order="3"></column></columns></table><table name="jmcode" text="居民代码表" database="jm" loadClass="com.dyna.report.config.H2load.JmCodeLoadCache">    <columns><column key="aaa102"  text="代码值" type="varchar(6)" order="0"></column><column key="aaa103" text="代码名称" type="varchar(100)" order="1"></column><column key="aaa100" text="代码" type="varchar(20)" order="2"></column><column key="aaz093" text="代码表id" type="long" order="3"></column></columns></table></h2>


sax  是基于事件解析的,即当读取到某一标签事件,开始读取文档事件,标签结束事件等,使用sax读取xml文件,需要继承 DefaultHandler

H2ConfigParse.java    内容                         读取xml文件的类  

 

package com.dyna.report.config.tools;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.apache.log4j.Logger;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.helpers.DefaultHandler;import com.dyna.report.config.abstracts.AbstractH2LoadCache;import com.dyna.report.config.exceptions.BussException;public class H2ConfigParse extends DefaultHandler {private static Logger logger = Logger.getLogger(H2ConfigParse.class.getName());private Map<String, H2Table> h2tables;private Map<String, H2Column> columns;private H2Table table;private H2Column column;/** * 加载文档事件 */public void startDocument() throws SAXException {logger.info("加载H2配置文件/WEB-INF/conf/H2LoadConfig.xml");h2tables = new HashMap<String, H2Table>();super.startDocument();}/** * 加载标签事件 */public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {logger.debug("开始读取标签"+qName);if("table".equalsIgnoreCase(qName)){table = new H2Table();table.setDatabase(attributes.getValue("database"));table.setName(attributes.getValue("name"));table.setText(attributes.getValue("text"));if(attributes.getValue("loadClass") != null &&!"".equals(attributes.getValue("loadClass"))){Object obj = Beanhelper.getClass(attributes.getValue("loadClass"));if(obj==null){throw new BussException(attributes.getValue("loadClass")+"在系统中未找到,请查验H2LoadConfig.xml配置是否正确");}if(! (obj instanceof AbstractH2LoadCache)){throw new BussException(attributes.getValue("loadClass")+"需继承自AbstractH2LoadCache,请查验该类是否正确");}   table.setLoadClass((AbstractH2LoadCache)obj);}}if("columns".equalsIgnoreCase(qName)){columns = new HashMap<String, H2Column>();}if("column".equalsIgnoreCase(qName)){column = new H2Column();column.setKey(attributes.getValue("key"));column.setText(attributes.getValue("text"));column.setType(attributes.getValue("type"));column.setOrder(attributes.getValue("order"));column.setTablename(table.getName());}super.startElement(uri, localName, qName, attributes);}/** * 加载标签完毕 */public void endElement(String uri, String localName, String qName)throws SAXException {if("table".equalsIgnoreCase(qName)){h2tables.put(table.getName(), table);table=null;}if("columns".equalsIgnoreCase(qName)){table.setColumns(columns);columns=null;}if("column".equalsIgnoreCase(qName)){columns.put(column.getKey(), column);column=null;}logger.debug("标签"+qName+"内容读取完毕");super.endElement(uri, localName, qName);}/** * 加载文档完毕 */public void endDocument() throws SAXException {// TODO Auto-generated method stublogger.info("配置文件/WEB-INF/conf/H2LoadConfig.xml 加载完成");}public Map<String, H2Table> getH2tables() {return h2tables;}@Overridepublic InputSource resolveEntity(String publicId, String systemId)throws IOException, SAXException {// TODO Auto-generated method stubreturn super.resolveEntity(publicId, systemId);}@Overridepublic void notationDecl(String name, String publicId, String systemId)throws SAXException {// TODO Auto-generated method stubsuper.notationDecl(name, publicId, systemId);}@Overridepublic void unparsedEntityDecl(String name, String publicId,String systemId, String notationName) throws SAXException {// TODO Auto-generated method stubsuper.unparsedEntityDecl(name, publicId, systemId, notationName);}@Overridepublic void setDocumentLocator(Locator locator) {// TODO Auto-generated method stubsuper.setDocumentLocator(locator);}@Overridepublic void startPrefixMapping(String prefix, String uri)throws SAXException {// TODO Auto-generated method stubsuper.startPrefixMapping(prefix, uri);}@Overridepublic void endPrefixMapping(String prefix) throws SAXException {// TODO Auto-generated method stubsuper.endPrefixMapping(prefix);}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {// TODO Auto-generated method stubsuper.characters(ch, start, length);}@Overridepublic void ignorableWhitespace(char[] ch, int start, int length)throws SAXException {// TODO Auto-generated method stubsuper.ignorableWhitespace(ch, start, length);}@Overridepublic void processingInstruction(String target, String data)throws SAXException {// TODO Auto-generated method stubsuper.processingInstruction(target, data);}@Overridepublic void skippedEntity(String name) throws SAXException {// TODO Auto-generated method stubsuper.skippedEntity(name);}@Overridepublic void warning(SAXParseException e) throws SAXException {// TODO Auto-generated method stubsuper.warning(e);}@Overridepublic void error(SAXParseException e) throws SAXException {// TODO Auto-generated method stubsuper.error(e);}@Overridepublic void fatalError(SAXParseException e) throws SAXException {// TODO Auto-generated method stubsuper.fatalError(e);}}


 

0 0
原创粉丝点击