1、读取本地xml文件
/**
* 加载本地配置文件
* @author HuangXuebin
* @date 2012-10-11
* @Version V 1.0
*/
public class LoadConfig {
private static Logger logger = Logger.getLogger(LoadConfig.class);
private static String localFilePatch;
private static String ftpUrl;
private static String ftpUser;
private static String ftpPassword;
private static String ftpFilePatch;
static {
load();//加载文件
}
private static void load() {
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(LoadConfig.class.getClassLoader().getResourceAsStream("systemConfig.xml"));//classpatch下的配置文件
Element root = doc.getRootElement();
Iterator iterlocal = root.elementIterator("local");//得到local节点
while (iterlocal.hasNext()) {
Element localEle = (Element) iterlocal.next();
localFilePatch = localEle.elementTextTrim("filePatch"); // 拿到local节点下的子节点的值
}
Iterator iterftp = root.elementIterator("ftp");//得到ftp节点
while (iterftp.hasNext()) {
Element ftpEle = (Element) iterftp.next();
ftpUrl = ftpEle.elementTextTrim("url"); // 拿到local节点下的子节点的值
ftpUser = ftpEle.elementTextTrim("user");
ftpPassword = ftpEle.elementTextTrim("password");
ftpFilePatch = ftpEle.elementTextTrim("filePatch");
}
} catch (DocumentException e) {
logger.info("========== 加载配置信息错误!==========");
throw new ExceptionInInitializerError(e);
}
}
public static String getLocalFilePatch() {
return localFilePatch;
}
public static void setLocalFilePatch(String localFilePatch) {
LoadConfig.localFilePatch = localFilePatch;
}
public static String getFtpUrl() {
return ftpUrl;
}
public static void setFtpUrl(String ftpUrl) {
LoadConfig.ftpUrl = ftpUrl;
}
public static String getFtpUser() {
return ftpUser;
}
public static void setFtpUser(String ftpUser) {
LoadConfig.ftpUser = ftpUser;
}
public static String getFtpPassword() {
return ftpPassword;
}
public static void setFtpPassword(String ftpPassword) {
LoadConfig.ftpPassword = ftpPassword;
}
public static String getFtpFilePatch() {
return ftpFilePatch;
}
public static void setFtpFilePatch(String ftpFilePatch) {
LoadConfig.ftpFilePatch = ftpFilePatch;
}
}
2、读取xml里嵌套的xml
/**
* 解析返回结果
* @param xml
* @return
*/
public Integer readStringXml(String xml) {
Document doc1 =null;
Document doc2 =null;
String MobileNum = "";
String SubOrderID = "";
String ItemCode = "";
Integer Result = 99;
try {
// 下面的是通过解析xml字符串的
doc1 = DocumentHelper.parseText(xml); // 将字符串转为XML
Element rootElt1 = doc1.getRootElement(); // 获取根节点
String SvcCont = rootElt1.elementTextTrim("SvcCont");// 获取根节点下的子节点SvcCont的值即内层xml
//进行第二次解析
doc2 = DocumentHelper.parseText(SvcCont); // 将字符串转为XML
Element rootElt2 = doc2.getRootElement(); // 获取根节点
//一下取得相应节点的值
MobileNum = rootElt2.elementTextTrim("MobileNum");
SubOrderID = rootElt2.elementTextTrim("SubOrderID");
ItemCode = rootElt2.elementTextTrim("ItemCode");
Result = Integer.parseInt(rootElt2.elementTextTrim("Result"));
//写入日志
logger.info("兑换云南省BOSS业务用户手机号:[" + MobileNum +"]订单号:["+SubOrderID+"商品编号:["+ItemCode+"]结果代码:["+Result+"]");
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return Result;
}