一、使用axis调用webService接口,返回String类型xml
1.导入axis依赖
2.直接贴代码
/**
* 调用webservice接口的方法,并返回String类型的xml
* @param readid 传入接口的参数
* @param url webservice的路径
* @return String类型的xml
* @throws Exception
*/
public static String getRainfall(String readid, String url) throws Exception {
//具体不知道干啥的,可以控制下方参数的类型
// OperationDesc oper=new OperationDesc();
// ParameterDesc param;
// oper.setName("方法名");
//创建service 实例
Service service = new Service();
//通过service 创建Call 实例
Call call = (Call) service.createCall();
//第一个参数命名空间,第二个参数方法名
QName qname = new QName("命名空间", "方法名"); //(targetNamespace, element name)
call.setOperationName(qname);//设置调用远程方法的路径 同 :call.setOperationName(new QName("http://WebXml.com.cn/","getTVprogramString"))
//将Web Service的服务路径加入到call实例之中
call.setTargetEndpointAddress(new URL(url));
//添加参数
call.addParameter("readid", XMLType.XSD_STRING, ParameterMode.IN);
//设置传入参数的类型,如果webservice已经确定参数类型,可不写,自己调用此方法时定义参数类型即可
//new QName("http://www.w3.org/2001/XMLSchema","int") ---http://www.w3.org/2001/XMLSchema 为int类型对应的url
// param = new ParameterDesc(new QName("命名空间",id),ParameterDesc.IN,new QName("http://www.w3.org/2001/XMLSchema","int"),int.class,false,false);
// param.setOmittable(true);
// oper.addParameter(param);
// param = new ParameterDesc(new QName("命名空间","readid"),ParameterDesc.IN,new QName("http://www.w3.org/2001/XMLSchema","String"),String.class,false,false);
// param.setOmittable(true);
// oper.addParameter(param);
//设置返回类型,不写会报错 我这里返回的是字符串
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
String Msg = (String) call.invoke(new Object[]{readid});
System.out.println(Msg);
return Msg;
}
二、使用dom4j简单解析xml
1.直接上代码
/**
* 解析String类型的xml
* @param strXML String类型的xml
* @throws Exception
*/
public static void getParam(String strXML) throws Exception {
Document document = DocumentHelper.parseText(strXML);
//根据节点对象获取相应信息
String ID = document.selectSingleNode("//ID").getText();//获取节点对象,注意引号内的“//”必须加 ,否则报错 水库id
String M1 = document.selectSingleNode("//M1").getText();
String M10 = document.selectSingleNode("//M10").getText();
String M60 = document.selectSingleNode("//M60").getText();
String D1 = document.selectSingleNode("//D1").getText();
String TOTAL = document.selectSingleNode("//TOTAL").getText();
String WATERLEVEL = document.selectSingleNode("//WATERLEVEL").getText();
String TEMPERATURE = document.selectSingleNode("//TEMPERATURE").getText();
String WORKVOL = document.selectSingleNode("//WORKVOL").getText();
String CHARGEVOL = document.selectSingleNode("//CHARGEVOL").getText();
String NETSTATUS = document.selectSingleNode("//NETSTATUS").getText();
String RAINSTATUS = document.selectSingleNode("//RAINSTATUS").getText();
String WATERSTATUS = document.selectSingleNode("//WATERSTATUS").getText();
String RAINTM = document.selectSingleNode("//RAINTM").getText();
String WATERTM = document.selectSingleNode("//WATERTM").getText();
String VOLTM = document.selectSingleNode("//VOLTM").getText();
String TM = document.selectSingleNode("//TM").getText();
System.out.println(":" + ID); //此时输出结果极为字符串:3333
System.out.println(":" + M1);
System.out.println(":" + M10);
System.out.println(":" + M60);
System.out.println(":" + D1);
System.out.println(":" + TOTAL);
System.out.println(":" + WATERLEVEL);
System.out.println(":" + TEMPERATURE);
System.out.println(":" + WORKVOL);
System.out.println(":" + CHARGEVOL);
System.out.println(":" + NETSTATUS);
System.out.println(":" + RAINSTATUS);
System.out.println(":" + WATERSTATUS);
System.out.println(":" + RAINTM);
System.out.println(":" + WATERTM);
System.out.println(":" + VOLTM);
System.out.println(":" + TM);
}
三、在main方法中上面两个方法测试
public static void main(String[] args) throws Exception {
//必填,否者报错: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
//xmlStr为上图格式的字符串
getParam(getRainfall("参数", "webService路径"));
}
测试结果: