2016-05-30 19 views
0

我使用Axis 1.4,并且我想在客户端中的SOAP主体的XML中插入额外的级别。有一个服务器响应,我可以用javax.xml.rpc.handler.GenericHandler的客户端中的子类得到:更改SOAP处理程序中的XML结构

现在,我尝试用

SOAPMessageContext smc = (SOAPMessageContext) context; 
SOAPMessage message = smc.getMessage(); 
SOAPBody sb = message.getSOAPBody(); 

NodeList nl = sb.getElementsByTagName("projectDataReturn"); 
if (nl.getLength() == 0) { 
    return true; // wrong message 
} 

log.info("we have a projectDataReturn structure"); 

NodeList cl = sb.getElementsByTagName("centres"); 
if (cl.getLength() == 0) { 
    return true; // no centres 
} 

log.info("we have centres tags"); 

在此认识到正确的信息类型点我需要一个新的标签,其中包含所有现有的<centres>标签。已存储在cl中的所有<centres>标记的列表已经存在,但我如何将新节点添加到<projectDataReturn>标记?以及如何将现有<centre>标签移动到新标签?我曾与

Document doc = cl.item(0).getOwnerDocument(); 

Element array = doc.createElement("centres"); 
array.setAttribute("xmlns:ns5", "http://beans.eo.xyz.de"); 
array.setAttribute("soapenc:arrayType", "ns5:CentreBean[" + cl.getLength() + "]"); 
array.setAttribute("xsi:type", "soapenc:Array"); 
array.setAttribute("xmlns:soapenc", "http://schemas.xmlsoap.org/soap/encoding/"); 

nl.item(0).appendChild(array); 

// move existing <centre> tags here 
return true; 

尝试过,但它产生的SoapException

javax.xml.rpc.JAXRPCException: javax.xml.soap.SOAPException: Could not get document from SOAPEnvelope 

有什么不对?

回答

0

我已经找到了解决办法:

Document doc = cl.item(0).getOwnerDocument(); 

Element array = doc.createElementNS("", "centres"); 
array.setAttributeNS("http://schemas.xmlsoap.org/soap/encoding/", "soapenc:arrayType", "CentreBean[" + cl.getLength() + "]"); 
array.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:soapenc", "http://schemas.xmlsoap.org/soap/encoding/"); 
array.setAttributeNS("http://www.w3.org/2000/xsi/", "xsi:type", "soapenc:Array"); 

现在我克隆老<centres>节点:

for (int i = 0; i < cl.getLength(); i++) { 
    array.appendChild(cl.item(i).cloneNode(true)); 
} 

现在我将新节点插入到XML(旧<centres>标签前) :

nl.item(0).insertBefore(array, cl.item(0)); 

然后我删除旧节点,这是错误的p花边:

for (int i = 0; i < cl.getLength(); i++) { 
    nl.item(0).removeChild(cl.item(i)); 
} 

如果您注册在客户端的服务存根的处理程序链中的处理程序代码,它只会被服务器响应的XML序列化之前执行:

import javax.xml.namespace.QName; 
import javax.xml.rpc.handler.HandlerInfo; 
import javax.xml.rpc.handler.HandlerRegistry; 

// uses Axis1 generated client classes 
MyService_Service serviceLocator = new MyService_ServiceLocator(); 

HandlerRegistry hr = serviceLocator.getHandlerRegistry(); 
List<HandlerInfo> handlerChain = hr.getHandlerChain((QName) serviceLocator.getPorts().next()); 

HandlerInfo hi = new HandlerInfo(); 
hi.setHandlerClass(MyXMLInjectionHandler.class); 
handlerChain.add(hi);