讀取xml檔案 解析雙層xml
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;
}
相關文章
- Java讀取多層級xml檔案JavaXML
- JAVA 讀取xml檔案JavaXML
- Java系列:讀取XML檔案JavaXML
- C#讀取Xml檔案C#XML
- 用JDOM讀取XML檔案XML
- SaxReader讀取,更新xml檔案XML
- XMl 檔案屬性的讀取XML
- NSXMLDocument讀取遠端XML檔案XML
- QDomDocument 讀取和編輯xml檔案XML
- jdom解析xml檔案XML
- GData解析XML檔案XML
- jquery 解析xml檔案jQueryXML
- Rss-Rome 閱讀器讀取XML檔案XML
- javascript讀取xml檔案程式碼例項JavaScriptXML
- 文摘:在EJB中讀取XML配置檔案XML
- js讀取xml檔案,用了Microsoft.XMLDOMJSXMLROS
- 簡單讀取XML檔案中的值XML
- jquery實現的讀取並解析xml檔案程式碼例項jQueryXML
- 使用 Java 解析XML檔案JavaXML
- 利用Perl解析XML檔案XML
- C#解析XML檔案C#XML
- python XML 檔案解析PythonXML
- 讀寫iOS XML檔案iOSXML
- XML 檔案解析實踐 (DOM 解析)XML
- Sql server:從XML檔案中讀取配置資訊SQLServerXML
- Android之XML檔案解析AndroidXML
- 使用PHP DOM-XML建立和解析XML檔案 (轉)PHPXML
- Unity 讀取xmlUnityXML
- js獲取並解析xml檔案程式碼例項JSXML
- SQL Server中讀取XML檔案的簡單做法SQLServerXML
- Python解析XML檔案生成HTMLPythonXMLHTML
- Java XML檔案解析書目錄JavaXML
- python 解析xml 檔案: SAX方式PythonXML
- python 解析xml 檔案: DOM 方式PythonXML
- Python解析xml大檔案(sax)PythonXML
- AndroidManifest.xml檔案解析AndroidXML
- xml檔案XML
- C#讀取XMLC#XML