教授写的读取xml文件信息实例
注释已到位,无需多写了!
ReadXMLBC .java
package lll;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* @since 20090818
* @author
* @todo read for xml
*
*/
public class ReadXMLBC {
//单例模式,只读取一次
private static ReadXMLBC _instance = null;
public static ReadXMLBC getInstance() {
if (_instance == null) {
arrayList = new ArrayList();
_instance = new ReadXMLBC();
}
return _instance;
}
private static ArrayList arrayList = null;
//构造函数
public ReadXMLBC() {
// 读文件构知生成List
// <FunMethod-case>
// <FunMethod>11002</FunMethod>
// <FunMethod-name>用户登录</FunMethod-name>
// <FunMethod-class>com.ptworldscmend.baseinfo.entinfo.LoginClass</FunMethod-class>
// <FunMethod-method>LoginTest</FunMethod-method>
// </FunMethod-case>
// <FunMethod-case>
// <FunMethod>11021</FunMethod>
// <FunMethod-name>用户登录</FunMethod-name>
// <FunMethod-class>com.ptworldscmend.baseinfo.entinfo.LoginClass</FunMethod-class>
// <FunMethod-method>LoginTest</FunMethod-method>
// </FunMethod-case>
// serviceNode oserviceNode = new serviceNode() ;
// oserviceNode.setServiceName("WebServicesWSPublic") ;
// oserviceNode.setFunMethod("100102") ;
// oserviceNode.setFunMethodName("用户登录") ;
// oserviceNode.setFunMethodClass("com.actnet.school.service.mgmt.schoolloginclass")
// ;
// oserviceNode.setFunMethodMethod("schoolLogin") ;
//
//
// arrayList.add(oserviceNode) ;
//
//
// oserviceNode = new serviceNode() ;
// oserviceNode.setServiceName("AllQueryWSSchool") ;
// oserviceNode.setFunMethod("130") ;
// oserviceNode.setFunMethodName("班级信息") ;
// oserviceNode.setFunMethodClass("com.actnet.school.service.mgmt.schoolClassbasicinfoclass")
// ;
// oserviceNode.setFunMethodMethod("schoolClassInfoQuery") ;
//
//
// arrayList.add(oserviceNode) ;
//
// oserviceNode = new serviceNode() ;
// oserviceNode.setServiceName("AllQueryWSSchool") ;
// oserviceNode.setFunMethod("120") ;
// oserviceNode.setFunMethodName("教室信息") ;
// oserviceNode.setFunMethodClass("com.actnet.school.service.mgmt.schoolroombasicinfoclass")
// ;
// oserviceNode.setFunMethodMethod("schoolRoomInfoQuery") ;
//
//
// arrayList.add(oserviceNode) ;
//
// File serviceproxyFile = new File(
// "D:/school/actnetschoolend/actnetschoolendinterface/src/serviceproxy.xml");
//Properties dd = System.getProperties() ;
GetListFromServiceProxy();
}
public static void GetListFromServiceProxy() {
ResourceBundle rb = null;
rb = ResourceBundle.getBundle(".configProxy");
if (rb == null) {
return;
}
String ServiceProxyFile = rb.getString("ServiceProxyFile");
File serviceproxyFile = new File(ServiceProxyFile);
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = builder.build(serviceproxyFile);
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
//根节点下子节点元素的
List serviceName_list = doc.getRootElement().getChildren();
List FunMethodCase_list;
String serviceNameValue = "";
String FunMethodValue = "";
String FunMethodNameValue = "";
String FunMethodClass = "";
String FunMethodMethodValue = "";
Element serviceNameElementNode;
Element FunMethodCaseElementNode;
for (Iterator i = serviceName_list.iterator(); i.hasNext();) { // 记录数
// 第i serviceName
serviceNameElementNode = (Element) i.next();
FunMethodCase_list = serviceNameElementNode.getChildren();
serviceNameValue = serviceNameElementNode.getAttributeValue("name")
.trim();
for (Iterator j = FunMethodCase_list.iterator(); j.hasNext();) {
FunMethodCaseElementNode = (Element) j.next();
FunMethodValue = FunMethodCaseElementNode.getChildText(
"FunMethod").trim();
FunMethodNameValue = FunMethodCaseElementNode.getChildText(
"FunMethod-name").trim();
FunMethodClass = FunMethodCaseElementNode.getChildText(
"FunMethod-class").trim();
FunMethodMethodValue = FunMethodCaseElementNode.getChildText(
"FunMethod-method").trim();
//定义ReadXMLBCPro属性类的对象
ReadXMLBCPro oserviceNode = new ReadXMLBCPro();
oserviceNode.setServiceName(serviceNameValue);
oserviceNode.setFunMethod(FunMethodValue);
oserviceNode.setFunMethodName(FunMethodNameValue);
oserviceNode.setFunMethodClass(FunMethodClass);
oserviceNode.setFunMethodMethod(FunMethodMethodValue);
//将返回的数据添加到结构体
arrayList.add(oserviceNode);
}
}
//清空所有new的对象
rb = null;
serviceproxyFile = null;
builder = null;
doc = null;
serviceName_list = null;
FunMethodCase_list = null;
serviceNameElementNode = null;
FunMethodCaseElementNode = null;
return;
}
/**
* 将返回的ArrayList的值进行遍历匹配,返回FunMethodClass值
* @param FunMethod
* @param service_name
* @return:FunMethodClass
*/
public static String GetClassNameByFunMethodAndServiceName(
String FunMethod, String service_name) {
if (arrayList == null) {
return null;
}
int arrayListLen = arrayList.size();
if (arrayListLen < 1) {
return null;
}
ReadXMLBCPro oserviceNode;
for (int i = 0; i < arrayListLen; i++) {
oserviceNode = (ReadXMLBCPro) arrayList.get(i);
if ((oserviceNode.getServiceName().equals(service_name))
&& (oserviceNode.getFunMethod().equals(FunMethod))) {
return oserviceNode.getFunMethodClass();
}
}
return null;
}
/**
* 将返回的ArrayList的值进行遍历匹配,返回FunMethodMethod值
* @param FunMethod
* @param service_name
* @return:FunMethodMethod
*/
public static String GetClassMethodByFunMethodAndServiceName(
String FunMethod, String service_name) {
if (arrayList == null) {
return null;
}
int arrayListLen = arrayList.size();
if (arrayListLen < 1) {
return null;
}
ReadXMLBCPro oserviceNode;
for (int i = 0; i < arrayListLen; i++) {
oserviceNode = (ReadXMLBCPro) arrayList.get(i);
if ((oserviceNode.getServiceName().equals(service_name))
&& (oserviceNode.getFunMethod().equals(FunMethod))) {
return oserviceNode.getFunMethodMethod();
}
}
return null;
}
}
ReadXMLBCPro .java
package lll;
public class ReadXMLBCPro {
private String ServiceName ;
private String FunMethod ;
private String FunMethodName ;
private String FunMethodClass ;
private String FunMethodMethod ;
public String getServiceName() {
return ServiceName;
}
public void setServiceName(String serviceName) {
ServiceName = serviceName;
}
public String getFunMethod() {
return FunMethod;
}
public void setFunMethod(String funMethod) {
FunMethod = funMethod;
}
public String getFunMethodName() {
return FunMethodName;
}
public void setFunMethodName(String funMethodName) {
FunMethodName = funMethodName;
}
public String getFunMethodClass() {
return FunMethodClass;
}
public void setFunMethodClass(String funMethodClass) {
FunMethodClass = funMethodClass;
}
public String getFunMethodMethod() {
return FunMethodMethod;
}
public void setFunMethodMethod(String funMethodMethod) {
FunMethodMethod = funMethodMethod;
}
}
xml文件:
<webservices>
<service>
<service-name>WebServicesWSFun_11</service-name>
<FunMethod-case>
<FunMethod>11002</FunMethod>
<FunMethod-name>用户登录</FunMethod-name>
<FunMethod-class>com.ptworldscmend.baseinfo.entinfo.LoginClass</FunMethod-class>
<FunMethod-method>LoginTest</FunMethod-method>
</FunMethod-case>
<FunMethod-case>
<FunMethod>11003</FunMethod>
<FunMethod-name>用户登录</FunMethod-name>
<FunMethod-class>com.ptworldscmend.baseinfo.entinfo.LoginClass</FunMethod-class>
<FunMethod-method>LoginTest</FunMethod-method>
</FunMethod-case>
<FunMethod-case>
<FunMethod>11003</FunMethod>
<FunMethod-name>用户登录</FunMethod-name>
<FunMethod-class>com.ptworldscmend.baseinfo.entinfo.LoginClass</FunMethod-class>
<FunMethod-method>LoginTest</FunMethod-method>
</FunMethod-case>
</service>
<service>
<service-name>WebServicesWSFun_21</service-name>
<FunMethod-case>
<FunMethod>11003</FunMethod>
<FunMethod-name>用户登录</FunMethod-name>
<FunMethod-class>com.ptworldscmend.baseinfo.entinfo.LoginClass</FunMethod-class>
<FunMethod-method>LoginTest</FunMethod-method>
</FunMethod-case>
</service>
<service>
<service-name>WebServicesWSFun_31</service-name>
</service>
</webservices>