import java.io.IOException; import java.io.StringReader; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.xml.sax.InputSource; public class Test3 { public static void main(String[] args) { System.out.println("================++++++++++==========="); try { Service service = new Service(); String url = "http://*****:8004/bianmin.asmx"; System.out.println("DMZ-url=========="+url); String soapaction = "http://www.bluebutterfly.cn/"; String funcname = "ShowInfoByCardIDName"; Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url); call.setOperationName(new QName(soapaction, funcname)); // 设置要调用哪个方法 call.addParameter( new QName(soapaction, "name"), // 设置要传递的参数 org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); call.addParameter( new QName(soapaction, "cardID"), // 设置要传递的参数 org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 返回类型 call.setUseSOAPAction(true); call.setSOAPActionURI(soapaction + funcname); String uname = "*涛"; String cardID = "37***11"; String v = (String) call.invoke(new Object[] { uname, cardID});// 调用方法并传递参数 System.out.println(v); System.out.println(xmlElements(v)); } catch (Exception ex) { ex.printStackTrace(); } } /** * 解析xml字符串返回一个Map * * @param xmlDoc * @return Map */ public static Map xmlElements(String xmlDoc) { Map map = new HashMap(); // 创建一个新的字符串 StringReader read = new StringReader(xmlDoc); // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入 InputSource source = new InputSource(read); // 创建一个新的SAXBuilder SAXBuilder sb = new SAXBuilder(); try { // 通过输入源构造一个Document Document doc = sb.build(source); // 取的根元素 Element root = doc.getRootElement(); // 得到根元素所有子元素的集合 List jiedian = root.getChildren(); // 获得XML中的命名空间(XML中未定义可不写) Element et = null; for (int i = 0; i < jiedian.size(); i++) { et = (Element) jiedian.get(i);// 循环依次得到子元素 map.put(et.getName(), et.getText()); } } catch (JDOMException e) { e.printStackTrace(); System.out.println("出错"); } catch (IOException e) { e.printStackTrace(); System.out.println("出错"); } read.close(); return map; } }
?