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 = "); 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