讀取xml檔案 解析雙層xml

sunnylovecmc發表於2012-10-15

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;
    }



相關文章