1 importjava.io.File;2 importjava.io.FileInputStream;3 importjava.util.List;4 importorg.jdom.Document;5 importorg.jdom.Element;6 importorg.jdom.input.SAXBuilder;7 importorg.xml.sax.InputSource;8
9 /**
10 * 作用: XML解析工具类,其中的属性根据自己需要另行添加或者更改11 *12 */
13 public classReadFileContent14 {15 staticFileInputStream ins;16
17 public static String trxId; //文件id
18 public static String trxBank; //银行编码
19 public static String trxOper; //20 public static String trxDate; //数据日期
21 public static String PkgNo; //包号
22 public static String fileCode; //文件编码
23 public static String fileName; //文件类型
24 public static String fileContent; //文件内容
25
26
27 public static voidPullConfigXml(String path)28 {29 Log4jBean.logger.info("开始读取配置文件...");30 try{31 File file=null;32 //本地测试路径 /home/ngpcom/dfgz/config33 //String path=System.getProperty("user.home")+file.separator+"dfgz"+file.separator+"config"+file.separator+"config1.xml";34 //String path =System.getProperty("user.home")+file.separator+"config"+file.separator+"config1.xml";
35 Log4jBean.logger.info("配置文件的路径["+path+"]");36 ins = new FileInputStream(newFile( path));37 } catch(Exception e) {38 Log4jBean.logger.error("读取配置文件异常,异常信息为:【" + e.getMessage() + "】");39 }40 Log4jBean.logger.info("读取配置文件成功,开始解析xml文档");41
42 //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML输入,此处为文件流
43 InputSource source = newInputSource(ins);44 //创建一个新的SAXBuilder
45 SAXBuilder saxbBuilder = newSAXBuilder();46 try{47 //通过输入源构造一个Document
48 Document doc =saxbBuilder.build(source);49 //取得xml根元素
50 Element root =doc.getRootElement();51 //取得根元素的子元素
52 List> node =root.getChildren();53 for (int i = 0; i < node.size(); i++) {54 Element element =(Element) node.get(i);55 if (element.getName().equals("trxId")) {56 trxId =element.getValue();57 } else if (element.getName().equals("trxBank")) {58 trxBank =element.getValue();59 } else if (element.getName().equals("trxOper")) {60 trxOper =element.getValue();61 } else if (element.getName().equals("trxDate")) {62 trxDate =element.getValue();63 } else if(element.getName().equals("PkgNo")){64 PkgNo=element.getValue();65 } else if(element.getName().equals("fileCode")){66 fileCode=element.getValue();67 } else if(element.getName().equals("fileName")){68 fileName=element.getValue();69 }else if(element.getName().equals("fileContent")){70 fileContent=element.getValue();71 }72 }73 Log4jBean.logger.info(" 解析xml配置文件成功");74 Log4jBean.logger.info("*****************************************************************************");75 Log4jBean.logger.info(" trxId:[" + trxId + "]");76 Log4jBean.logger.info(" trxBank:[" + trxBank + "]");77 Log4jBean.logger.info(" trxOper:[" + trxOper + "]");78 Log4jBean.logger.info(" trxDate:[" + trxDate + "]");79 Log4jBean.logger.info(" PkgNo:[" + PkgNo + "]");80 Log4jBean.logger.info(" fileCode:[" + fileCode + "]");81 Log4jBean.logger.info(" fileName:[" + fileName + "]");82 Log4jBean.logger.info(" fileContent:[" + fileContent + "]");83 Log4jBean.logger.info("*****************************************************************************");84 } catch(Exception e) {85 Log4jBean.logger.error("解析xml配置文件异常,异常信息为:【" + e.getMessage() + "】");86 }87
88 }89 public static voidmain(String[] args)90 {91 //PullConfigXml();
92 }