dom4j解析soap xml

做项目,客户给的webService接口,传入一个用户名,来获取数据。先用的

RPCServiceClient serviceClient = new RPCServiceClient();  
Options options = serviceClient.getOptions(); 
EndpointReference targetEPR = new EndpointReference( "http://XXXXXXX");  
options.setTo(targetEPR);  
Object[] opAddEntryArgs = new Object[] {UserID}; 
Class[] classes = new Class[] {Task.class};//此处的Task为返回的对象  
QName opAddEntry = new QName("http://XXXXX", "XXX");

但这样用一直报错。说预期的参数为{}UserID……,所以用了HttpClient方法,如下:

//此处的Document为dom4j的Document
Document responseStr = null;
    //此处的userName就是需要传入的参数,XXX为targetNamespace,其他都是固定的
        String soapRequestData=
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ust=\""+XXX+">\n" +
                    "   <soapenv:Header/>\n" + 
                    "   <soapenv:Body>\n" + 
                    "      <ust:getNewTasks>\n" + 
                    "         <UserID>"+ userName +"</UserID>\n" + 
                    "      </ust:getNewTasks>\n" + 
                    "   </soapenv:Body>\n" + 
                    "</soapenv:Envelope>";
        try {
        //此处的url为webService接口,但不需要最后的/wsdl
            responseStr=invokeWSMethod(soapRequestData, url,"urn:#getNewTasks");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这样就返回了一个Document的xml数据,数据如下:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:root xmlns:ns2="http://www.ustcsoft.com">
            <ns2:task>
                <userid>myc</userid>
                <custname>你好你好你好</custname>
                <title>阿拉啦啦啦啦</title>
                <nodename>金丝能接受是</nodename>
                <recetime>201701160949</recetime>
                <tasktype>1</tasktype>
            </ns2:task>
        </ns2:root>
    </soap:Body>
</soap:Envelope>

此处的task就是要返回的对象,task可能有多个。经过查询之后找到这样一个方法:

public static List<Task> xmlToBean(Document doc) {
        List<Task> returnTaskList = new ArrayList<Task>();
        Task task = null;
        doc.getText();
        try{
            String text = doc.asXML();
            DefaultXPath xpath = new DefaultXPath("//ns2:task");
            xpath.setNamespaceURIs(Collections.singletonMap("ns2","http://www.ustcsoft.com")); 
            //先获取所有的task
            List<Element> list = xpath.selectNodes(doc); 
            if(list.size() > 0) {
                for(int j=0; j<list.size(); j++) {
                    task = new Task();
                    //获取task下的所有子节点
                    Element element = list.get(j);
                    //遍历每一个子节点
                    for(Iterator<Element> it = element.elementIterator(); it.hasNext();) {
                        Element element2 = it.next();
                        String name = element2.getName();
                        String content = element2.getText();
                        //下面的操作是因为每一个task我只需要custname、title、nodename这三个字段信息
                        if("custname".equals(name)) {
                            task.setCustname(content);
                        } else if("title".equals(name)) {
                            task.setTitle(content);
                        } else if("nodename".equals(name)) {
                            task.setNodename(content);
                        }
                    }
                    returnTaskList.add(task);
                }
            }
        }catch(Exception e) {
            log.info("", e);
        }
        return returnTaskList;
    }

这样就将一个soap的xml数据解析成为一个对象了。
【注:解析xml是从网上搜的方法,由于是几天前搜的,所以忘了地址。】

猜你喜欢

转载自blog.csdn.net/myc_csdn/article/details/54598407