SAXReader
说明:SAXReader主要用于解析XML文件,是当前比较流行的xml解决方案.
代码以基础类函数的形式展现如下:
java 代码
package com.dc.framework.taglib;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
//自定义标签处理类
public class ReadUploadFile {
// 配置文件的路径
static String uploadCfgFile = "WEB-INF/conf/uploadfile.xml";
//文档对象实体
Document uploadDocument = null;
//结果存储体
Map uploadMap = new HashMap();
/**
* @param request
* @return
* 获取配置文件绝对路径
*/
private String getuploadCfgFile(ServletRequest request) {
return ((HttpServletRequest) request).getSession().getServletContext().getRealPath(uploadCfgFile);
}
/**
* @param request
* @return
* 初始化配置文件
*/
private Document inituploadCfgDocument(ServletRequest request) {
SAXReader saxReader = new SAXReader();
try {
//得到指定文件以流的形式对文件进行序列化
FileInputStream fis = new FileInputStream(getuploadCfgFile(request));
uploadDocument = saxReader.read(fis);
}
catch (DocumentException e) {
e.printStackTrace();
}
catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}
return uploadDocument;
}
/**
* 获取配置文件
*
* @param request
* @return
*/
private Document getuploadCfgDocument(ServletRequest request) {
if (this.uploadDocument == null) {
this.uploadDocument = inituploadCfgDocument(request);
}
return this.uploadDocument;
}
/**
* @param request
* @return
* 获取某个指定preparents的配置属性
*/
public Map getuploadMap(ServletRequest request) {
//xml中的访问路径,说明欲处理XML文件的根元素是fileaction-configuration
String xPath = "//fileaction-configuration/preparents";
//获取按照指定搜索路径得到的元素列表,这里指获取所有的fileaction-configuration下的preparents元素
List result = getuploadCfgDocument(request).selectNodes(xPath);
//属性和指定子元素的存储体
Map uploadMap = new HashMap();
//遍历所得元素
Iterator iter = result.iterator();
while (iter.hasNext()) {
//获得具体的preparents元素
Element preparents = (Element) iter.next();
// 取得preparents元素的id属性的值
String id2 = preparents.attributeValue("id");
// 取得preparents元素下子元素tabname的值,并与id2配对存储
uploadMap.put(id2,
StringUtils.trim(preparents.elementText("tabname")));
}
return uploadMap;
}
}
代码以基础类函数的形式展现如下:
java 代码
package com.dc.framework.taglib;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
//自定义标签处理类
public class ReadUploadFile {
// 配置文件的路径
static String uploadCfgFile = "WEB-INF/conf/uploadfile.xml";
//文档对象实体
Document uploadDocument = null;
//结果存储体
Map uploadMap = new HashMap();
/**
* @param request
* @return
* 获取配置文件绝对路径
*/
private String getuploadCfgFile(ServletRequest request) {
return ((HttpServletRequest) request).getSession().getServletContext().getRealPath(uploadCfgFile);
}
/**
* @param request
* @return
* 初始化配置文件
*/
private Document inituploadCfgDocument(ServletRequest request) {
SAXReader saxReader = new SAXReader();
try {
//得到指定文件以流的形式对文件进行序列化
FileInputStream fis = new FileInputStream(getuploadCfgFile(request));
uploadDocument = saxReader.read(fis);
}
catch (DocumentException e) {
e.printStackTrace();
}
catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}
return uploadDocument;
}
/**
* 获取配置文件
*
* @param request
* @return
*/
private Document getuploadCfgDocument(ServletRequest request) {
if (this.uploadDocument == null) {
this.uploadDocument = inituploadCfgDocument(request);
}
return this.uploadDocument;
}
/**
* @param request
* @return
* 获取某个指定preparents的配置属性
*/
public Map getuploadMap(ServletRequest request) {
//xml中的访问路径,说明欲处理XML文件的根元素是fileaction-configuration
String xPath = "//fileaction-configuration/preparents";
//获取按照指定搜索路径得到的元素列表,这里指获取所有的fileaction-configuration下的preparents元素
List result = getuploadCfgDocument(request).selectNodes(xPath);
//属性和指定子元素的存储体
Map uploadMap = new HashMap();
//遍历所得元素
Iterator iter = result.iterator();
while (iter.hasNext()) {
//获得具体的preparents元素
Element preparents = (Element) iter.next();
// 取得preparents元素的id属性的值
String id2 = preparents.attributeValue("id");
// 取得preparents元素下子元素tabname的值,并与id2配对存储
uploadMap.put(id2,
StringUtils.trim(preparents.elementText("tabname")));
}
return uploadMap;
}
}