java读取xml方法



java读取xml方法实例介绍。

1.java读取xml文件

package com.trendcom.freemarker.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.trendcom.freemarker.view.DynamicConfig;
import com.trendcom.freemarker.view.HtmlConfig;
import com.trendcom.freemarker.view.MailConfig;
import com.trendcom.oscache.util.OsCacheConfigUtil;

public class TemplateConfigUtil {

public static final String DYNAMIC_CONFIG_LIST_CACHE_KEY = “dynamicConfigList”;// 动态模板配置缓存Key
public static final String HTML_CONFIG_LIST_CACHE_KEY = “htmlConfigList”;// 生成静态模板配置缓存Key
public static final String MAIL_CONFIG_LIST_CACHE_KEY = “mailConfigList”;// 邮件模板配置缓存Key

public static final String NODE_DYNAMICCONFIG=”/trendcom/dynamicConfig”;

/**
* 获取动态模板配置
*
* @return DynamicConfig集合
*/
@SuppressWarnings(“unchecked”)
public static List<DynamicConfig> getDynamicConfigList(String templateXmlPath) {
List<DynamicConfig> dynamicConfigList = null;//(List<DynamicConfig>) OsCacheConfigUtil.getFromCache(DYNAMIC_CONFIG_LIST_CACHE_KEY);
if (dynamicConfigList != null) {
return dynamicConfigList;
}
File configFile = null;
Document document = null;
try {
String configFilePath = new File(Thread.currentThread().getContextClassLoader().getResource(“”).toURI().getPath()).getParent() +templateXmlPath;
configFile = new File(configFilePath);
SAXReader saxReader = new SAXReader();
document = saxReader.read(configFile);
} catch (Exception e) {
e.printStackTrace();
}
Element htmlConfigElement = (Element)document.selectSingleNode(NODE_DYNAMICCONFIG);
Iterator<Element> iterator = htmlConfigElement.elementIterator();
dynamicConfigList = new ArrayList<DynamicConfig>();
while(iterator.hasNext()) {
Element element = (Element)iterator.next();
String description = element.element(“description”).getTextTrim();
String templateFilePath = element.element(“templateFilePath”).getTextTrim();
String htmlFilePath=element.element(“htmlFilePath”).getTextTrim();
String className=element.element(“className”).getTextTrim();
String method=element.element(“method”).getTextTrim();
String templateId=element.element(“templateId”).getTextTrim();
String blockName=element.element(“blockName”).getTextTrim();
DynamicConfig dynamicConfig = new DynamicConfig();
dynamicConfig.setName(element.getName());
dynamicConfig.setDescription(description);
dynamicConfig.setTemplateFilePath(templateFilePath);
dynamicConfig.setClassName(className);
dynamicConfig.setMethod(method);
dynamicConfig.setHtmlFilePath(htmlFilePath);
dynamicConfig.setTemplateId(templateId);
dynamicConfig.setBlockName(blockName);
dynamicConfigList.add(dynamicConfig);

}
OsCacheConfigUtil.putInCache(DYNAMIC_CONFIG_LIST_CACHE_KEY, dynamicConfigList);
return dynamicConfigList;
}

/**
* 根据动态模板配置名称获取DynamicConfig对象
*
* @return DynamicConfig对象
*/
public static DynamicConfig getDynamicConfig(String templateXmlPath,String name) {
Document document = null;
try {
String configFilePath = new File(Thread.currentThread().getContextClassLoader().getResource(“”).toURI().getPath()).getParent() + templateXmlPath;
File configFile = new File(configFilePath);
SAXReader saxReader = new SAXReader();
document = saxReader.read(configFile);
} catch (Exception e) {
e.printStackTrace();
}
Element element = (Element)document.selectSingleNode(NODE_DYNAMICCONFIG +”/”+name);
String description = element.element(“description”).getTextTrim();
String templateFilePath = element.element(“templateFilePath”).getTextTrim();
DynamicConfig dynamicConfig = new DynamicConfig();
dynamicConfig.setName(element.getName());
dynamicConfig.setDescription(description);
dynamicConfig.setTemplateFilePath(templateFilePath);
String htmlFilePath=element.element(“htmlFilePath”).getTextTrim();
String className=element.element(“className”).getTextTrim();
String method=element.element(“method”).getTextTrim();
String templateId=element.element(“templateId”).getTextTrim();
String blockName=element.element(“blockName”).getTextTrim();
dynamicConfig.setClassName(className);
dynamicConfig.setMethod(method);
dynamicConfig.setHtmlFilePath(htmlFilePath);
dynamicConfig.setTemplateId(templateId);
dynamicConfig.setBlockName(blockName);
return dynamicConfig;
}

/**
* 根据DynamicConfig对象读取模板文件内容
*
* @return 模板文件内容
*/
public static String readTemplateFileContent(DynamicConfig dynamicConfig) {
ServletContext servletContext = ServletActionContext.getServletContext();
File templateFile = new File(servletContext.getRealPath(dynamicConfig.getTemplateFilePath()));
String templateFileContent = null;
try {
templateFileContent = FileUtils.readFileToString(templateFile, “UTF-8″);
} catch (IOException e) {
e.printStackTrace();
}
return templateFileContent;
}


/**
* 写入模板文件内容
*
*/
public static String writeTemplateFileContent(DynamicConfig dynamicConfig, String templateFileContent) {
ServletContext servletContext = ServletActionContext.getServletContext();
File templateFile = new File(servletContext.getRealPath(dynamicConfig.getTemplateFilePath()));
try {
FileUtils.writeStringToFile(templateFile, templateFileContent, “UTF-8″);
} catch (IOException e) {
e.printStackTrace();
}
return templateFileContent;
}

/**
* 获取生成静态模板配置
*
* @return HtmlConfig集合
*/
@SuppressWarnings(“unchecked”)
public static List<HtmlConfig> getHtmlConfigList(String templateXmlPath) {
List<HtmlConfig> htmlConfigList = (List<HtmlConfig>) OsCacheConfigUtil.getFromCache(HTML_CONFIG_LIST_CACHE_KEY);
if (htmlConfigList != null) {
return htmlConfigList;
}
File configFile = null;
Document document = null;
try {
String configFilePath = new File(Thread.currentThread().getContextClassLoader().getResource(“”).toURI().getPath()).getParent() +templateXmlPath;
configFile = new File(configFilePath);
SAXReader saxReader = new SAXReader();
document = saxReader.read(configFile);
} catch (Exception e) {
e.printStackTrace();
}
Element htmlConfigElement = (Element)document.selectSingleNode(“/trendcom/htmlConfig”);
Iterator<Element> iterator = htmlConfigElement.elementIterator();
htmlConfigList = new ArrayList<HtmlConfig>();
while(iterator.hasNext()) {
Element element = (Element)iterator.next();
String description = element.element(“description”).getTextTrim();
String templateFilePath = element.element(“templateFilePath”).getTextTrim();
String htmlFilePath = element.element(“htmlFilePath”).getTextTrim();
HtmlConfig htmlConfig = new HtmlConfig();
htmlConfig.setName(element.getName());
htmlConfig.setDescription(description);
htmlConfig.setTemplateFilePath(templateFilePath);
htmlConfig.setHtmlFilePath(htmlFilePath);
htmlConfigList.add(htmlConfig);
}
OsCacheConfigUtil.putInCache(HTML_CONFIG_LIST_CACHE_KEY, htmlConfigList);
return htmlConfigList;
}

/**
* 根据生成静态模板配置名称获取HtmlConfig对象
*
* @return HtmlConfig对象
*/
public static HtmlConfig getHtmlConfig(String templateXmlPath,String name) {
Document document = null;
try {
String configFilePath = new File(Thread.currentThread().getContextClassLoader().getResource(“”).toURI().getPath()).getParent()+ templateXmlPath;
File configFile = new File(configFilePath);
SAXReader saxReader = new SAXReader();
document = saxReader.read(configFile);
} catch (Exception e) {
e.printStackTrace();
}
Element element = (Element)document.selectSingleNode(“/trendcom/htmlConfig/” + name);
String description = element.element(“description”).getTextTrim();
String templateFilePath = element.element(“templateFilePath”).getTextTrim();
String htmlFilePath = element.element(“htmlFilePath”).getTextTrim();
HtmlConfig htmlConfig = new HtmlConfig();
htmlConfig.setName(element.getName());
htmlConfig.setDescription(description);
htmlConfig.setTemplateFilePath(templateFilePath);
htmlConfig.setHtmlFilePath(htmlFilePath);
return htmlConfig;
}

/**
* 根据HtmlConfig对象读取模板文件内容
*
* @return 模板文件内容
*/
public static String readTemplateFileContent(HtmlConfig htmlConfig) {
ServletContext servletContext = ServletActionContext.getServletContext();
File templateFile = new File(servletContext.getRealPath(htmlConfig.getTemplateFilePath()));
String templateFileContent = null;
try {
templateFileContent = FileUtils.readFileToString(templateFile, “UTF-8″);
} catch (IOException e) {
e.printStackTrace();
}
return templateFileContent;
}

/**
* 写入模板文件内容
*
*/
public static String writeTemplateFileContent(HtmlConfig htmlConfig, String templateFileContent) {
ServletContext servletContext = ServletActionContext.getServletContext();
File templateFile = new File(servletContext.getRealPath(htmlConfig.getTemplateFilePath()));
try {
FileUtils.writeStringToFile(templateFile, templateFileContent, “UTF-8″);
} catch (IOException e) {
e.printStackTrace();
}
return templateFileContent;
}

/**
* 获取邮件模板配置
*
* @return MailConfig集合
*/
@SuppressWarnings(“unchecked”)
public static List<MailConfig> getMailConfigList(String templateXmlPath) {
List<MailConfig> mailConfigList = (List<MailConfig>) OsCacheConfigUtil.getFromCache(MAIL_CONFIG_LIST_CACHE_KEY);
if (mailConfigList != null) {
return mailConfigList;
}
Document document = null;
try {
String configFilePath = new File(Thread.currentThread().getContextClassLoader().getResource(“”).toURI().getPath()).getParent() + templateXmlPath;
File configFile = new File(configFilePath);
SAXReader saxReader = new SAXReader();
document = saxReader.read(configFile);
} catch (Exception e) {
e.printStackTrace();
}
Element mailConfigElement = (Element)document.selectSingleNode(“/trendcom/mailConfig”);
Iterator<Element> iterator = mailConfigElement.elementIterator();
mailConfigList = new ArrayList<MailConfig>();
while(iterator.hasNext()) {
Element element = (Element)iterator.next();
String description = element.element(“description”).getTextTrim();
String subject = element.element(“subject”).getTextTrim();
String templateFilePath = element.element(“templateFilePath”).getTextTrim();
MailConfig mailConfig = new MailConfig();
mailConfig.setName(element.getName());
mailConfig.setDescription(description);
mailConfig.setSubject(subject);
mailConfig.setTemplateFilePath(templateFilePath);
mailConfigList.add(mailConfig);
}
OsCacheConfigUtil.putInCache(MAIL_CONFIG_LIST_CACHE_KEY, mailConfigList);
return mailConfigList;
}

/**
* 根据邮件模板配置名称获取MailConfig对象
*
* @return MailConfig对象
*/
public static MailConfig getMailConfig(String templateXmlPath, String name) {
Document document = null;
try {
String configFilePath = new File(Thread.currentThread().getContextClassLoader().getResource(“”).toURI().getPath()).getParent()+templateXmlPath;
File configFile = new File(configFilePath);
SAXReader saxReader = new SAXReader();
document = saxReader.read(configFile);
} catch (Exception e) {
e.printStackTrace();
}
Element element = (Element)document.selectSingleNode(“/trendcom/mailConfig/” + name);
String description = element.element(“description”).getTextTrim();
String subject = element.element(“subject”).getTextTrim();
String templateFilePath = element.element(“templateFilePath”).getTextTrim();
MailConfig mailConfig = new MailConfig();
mailConfig.setName(element.getName());
mailConfig.setDescription(description);
mailConfig.setSubject(subject);
mailConfig.setTemplateFilePath(templateFilePath);
return mailConfig;
}

/**
* 根据MailConfig对象读取模板文件内容
*
* @return 模板文件内容
*/
public static String readTemplateFileContent(MailConfig mailConfig) {
ServletContext servletContext = ServletActionContext.getServletContext();
File templateFile = new File(servletContext.getRealPath(mailConfig.getTemplateFilePath()));
String templateFileContent = null;
try {
templateFileContent = FileUtils.readFileToString(templateFile, “UTF-8″);
} catch (IOException e) {
e.printStackTrace();
}
return templateFileContent;
}

/**
* 写入模板文件内容
*
*/
public static String writeTemplateFileContent(MailConfig mailConfig, String templateFileContent) {
ServletContext servletContext = ServletActionContext.getServletContext();
File templateFile = new File(servletContext.getRealPath(mailConfig.getTemplateFilePath()));
try {
FileUtils.writeStringToFile(templateFile, templateFileContent, “UTF-8″);
} catch (IOException e) {
e.printStackTrace();
}
return templateFileContent;
}

}

2.xml配置文件

<?xml version=”1.0″ encoding=”UTF-8″?>
<trendcom>
<description>云号通 模板配置文件</description>

<dynamicConfig>
<rollPicture>
<description>图片滚动</description>
<templateFilePath>/WEB-INF/template/cac/RollPicture.ftl</templateFilePath>
<htmlFilePath>/cac/webs/CACMain/rollPicture_cac.html</htmlFilePath>
<className>com.trendcom.portal.html.CreateUniqueBlockService</className>
<method>buildHtml</method>
<templateId>167</templateId>
<blockName>rollPicture_cac</blockName>
</rollPicture>
<rollPicture>
<description>新闻和公告</description>
<templateFilePath>/WEB-INF/template/cac/NewsAnnouncement.ftl</templateFilePath>
<htmlFilePath>/cac/webs/CACMain/NewsAnnouncement.html</htmlFilePath>
<className>com.trendcom.portal.html.CreateUniquePBlockService</className>
<method>buildHtml</method>
<templateId>167</templateId>
<blockName>NewsAnnouncement</blockName>
</rollPicture>

</dynamicConfig>
<htmlConfig>
<baseJavascript>
<description>baseJavascript</description>
<templateFilePath>/WEB-INF/template/common/base_javascript.ftl</templateFilePath>
<htmlFilePath>/template/common/js/base.js</htmlFilePath>
</baseJavascript>
<errorPage>
<description>错误页</description>
<templateFilePath>/WEB-INF/template/common/error_page.ftl</templateFilePath>
<htmlFilePath>/html/error_page.html</htmlFilePath>
</errorPage>
<errorPageAccessDenied>
<description>权限错误页</description>
<templateFilePath>/WEB-INF/template/common/error_page.ftl</templateFilePath>
<htmlFilePath>/html/error_page_access_denied.html</htmlFilePath>
</errorPageAccessDenied>
<errorPage500>
<description>错误页500</description>
<templateFilePath>/WEB-INF/template/common/error_page.ftl</templateFilePath>
<htmlFilePath>/html/error_page_500.html</htmlFilePath>
</errorPage500>
<errorPage404>
<description>错误页404</description>
<templateFilePath>/WEB-INF/template/common/error_page.ftl</templateFilePath>
<htmlFilePath>/html/error_page_404.html</htmlFilePath>
</errorPage404>
<errorPage403>
<description>错误页403</description>
<templateFilePath>/WEB-INF/template/common/error_page.ftl</templateFilePath>
<htmlFilePath>/html/error_page_403.html</htmlFilePath>
</errorPage403>
</htmlConfig>
<mailConfig>
<smtpTest>
<description>SMTP邮箱配置测试</description>
<subject>SMTP邮箱配置测试</subject>
<templateFilePath>/WEB-INF/template/shop/mail_smtp_test.ftl</templateFilePath>
</smtpTest>
<passwordRecover>
<description>密码找回</description>
<subject>密码找回</subject>
<templateFilePath>/WEB-INF/template/shop/mail_password_recover.ftl</templateFilePath>
</passwordRecover>
</mailConfig>
</trendcom>