1.直接代碼部分:
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.util.List; 4 import org.jdom.Document; 5 import org.jdom.Element; 6 import org.jdom.input.SAXBuilder; 7 import org.xml.sax.InputSource; 8 9 /** 10 * 作用: XML解析工具類,其中的屬性根據自己需要另行添加或者更改 11 * 12 */ 13 public class ReadFileContent 14 { 15 static FileInputStream 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 void PullConfigXml(String path) 28 { 29 Log4jBean.logger.info("開始讀取配置文件..."); 30 try { 31 File file=null; 32 //本地測試路徑 /home/ngpcom/dfgz/config 33 //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(new File( 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 = new InputSource(ins); 44 // 創建一個新的SAXBuilder 45 SAXBuilder saxbBuilder = new SAXBuilder(); 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 void main(String[] args) 90 { 91 //PullConfigXml(); 92 }
