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

前端之家收集整理的这篇文章主要介绍了H2通用缓存加载(2)——使用sax解析xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 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,qName);
	}

	/**
	 * 加载文档完毕
	 */
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		logger.info("配置文件/WEB-INF/conf/H2LoadConfig.xml 加载完成");
	}

	public Map<String,H2Table> getH2tables() {
		return h2tables;
	}

	@Override
	public InputSource resolveEntity(String publicId,String systemId)
			throws IOException,SAXException {
		// TODO Auto-generated method stub
		return super.resolveEntity(publicId,systemId);
	}

	@Override
	public void notationDecl(String name,String publicId,String systemId)
			throws SAXException {
		// TODO Auto-generated method stub
		super.notationDecl(name,publicId,systemId);
	}

	@Override
	public void unparsedEntityDecl(String name,String systemId,String notationName) throws SAXException {
		// TODO Auto-generated method stub
		super.unparsedEntityDecl(name,systemId,notationName);
	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		super.setDocumentLocator(locator);
	}

	@Override
	public void startPrefixMapping(String prefix,String uri)
			throws SAXException {
		// TODO Auto-generated method stub
		super.startPrefixMapping(prefix,uri);
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		super.endPrefixMapping(prefix);
	}

	@Override
	public void characters(char[] ch,int start,int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch,start,length);
	}

	@Override
	public void ignorableWhitespace(char[] ch,int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.ignorableWhitespace(ch,length);
	}

	@Override
	public void processingInstruction(String target,String data)
			throws SAXException {
		// TODO Auto-generated method stub
		super.processingInstruction(target,data);
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		super.skippedEntity(name);
	}

	@Override
	public void warning(SAXParseException e) throws SAXException {
		// TODO Auto-generated method stub
		super.warning(e);
	}

	@Override
	public void error(SAXParseException e) throws SAXException {
		// TODO Auto-generated method stub
		super.error(e);
	}

	@Override
	public void fatalError(SAXParseException e) throws SAXException {
		// TODO Auto-generated method stub
		super.fatalError(e);
	}

}
原文链接:https://www.f2er.com/xml/297583.html

猜你在找的XML相关文章