1、这是我定义的xml,test.xml
<PathDefines>
<PathDefine name="***" paperType="P">
<printType name="test1" url="/paper-print.jsp" printModeId="1"></printType>
<printType name="test2" url="/paper-print0.jsp" printModeId="2"></printType>
</PathDefine>
</PathDefines>
2、为了获取xml中的url,写的类如下:
public class Test{
public Test(){
}
/**
* 根据传入的参数,找到对应的url并返回
* @param paperType单据类型
* @param parameterId 打印类型参数
* return 返回当前路径
*/
public String getPageUrl(String paperType,String printModeId){
try{
ClassLoader cl=Thread.currentThread().getContextClassLoader();
SAXReader reader=new SAXReader();
Document document=reader.read(cl.getResourceAsStream("test.xml"));
Iterator iter=document.selectNodes("/PathDefines/PathDefine[@paperType='"+paperType+"']/printType[@printModeId='"+printModeId+"']").iterator();
String result="";
if (iter.hasNext())
result=((Element)iter.next()).attributeValue("url");
return result;
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
return "";
}
public static void main(String[] args){
PrintPath printPath=new PrintPath();
String url=printPath.getPageUrl("P", "1");
System.out.println(url);
}
}