xml通用解析类

package com.poson.cb.util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.poson.cb.dao.impl.sidmngr.XmlConfig;

/**
* Title: XmlParse
* Description: 用来解析配置了SQL脚本的XMl文件,如fileClassCount.xml
* Copyright: Copyright (c) 2007
* Company: POSON
* @author jczhangb
* @version 1.0
*/
public class XmlParse {
public static String File_Path;
public Document document;
private XmlParse() throws Exception{
SAXBuilder saBuilder=new SAXBuilder();
document=saBuilder.build(File_Path);
}

public static XmlParse getInstance() throws Exception{
File_Path=getCurrentPath()+File.separator+"fileClassCountSql.xml";
return new XmlParse();
}

public static XmlParse getInstance(String filePath) throws Exception{
File_Path=filePath;
return new XmlParse();
}

/**
* @parmer: id
* @return :返回对应的SQL脚本
* @author jczhangb
* @version 1.0
*/
public String getTextContentById(String attrName) throws Exception{
/*SAXBuilder saBuilder=new SAXBuilder();
org.jdom.Document document=saBuilder.build(getCurrentPath()+File.separator+fileName);*/

String sqlString="";
Element root=document.getRootElement();
List list=root.getChildren();
for (int i = 0; i < list.size(); i++) {
Element element=(Element)list.get(i);
if(element.getAttributeValue("id").equals(attrName)){
sqlString=element.getValue();
return sqlString;
}
}
return sqlString;
}

// 用来定位路径
static public String getCurrentPath() throws UnsupportedEncodingException {
String path = XmlConfig.class.getProtectionDomain().getCodeSource()
.getLocation().getFile();
path = java.net.URLDecoder.decode(path, "UTF-8");
int index = path.lastIndexOf("/classes");
path = path.substring(0, index);

System.out.println("CB xml path = [ " + path + " ]");

return path;
}

public static void main(String[] args) {
try {

System.out.println(XmlParse.getInstance().getTextContentById("ivpnTruncateSql"));
} catch (Exception e) {
System.out.println("没找到");
}
}
}


2013-08-01 19:40 记 @jinrongdajie31.xichengqu.beijing
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值