Changing XML structure in SOAP Handler

…衆ロ難τιáo~ 提交于 2019-12-12 03:06:52

问题


I use Axis 1.4 and I want to insert an additional level within in the XML of a SOAP body within the client. There is a server response, which I can get with a subclass of javax.xml.rpc.handler.GenericHandler in the client:

Now I try to recognize the right message type with

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");

At this point I need a new tag, which holds all existing <centres> tags. The list of all <centres> tags I have stored in cl already, but how I can add new nodes to the <projectDataReturn> tag? And how I can move the existing <centre> tags to the new tag?. I have tried it with

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;

But it produces a SOAPException

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

What's wrong?


回答1:


I have found a solution:

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");

Now I clone the old <centres> nodes:

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

Now I insert the new node into the XML (in front of the old <centres> tags):

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

And I remove the old nodes, which are on the wrong place:

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

If you register the handler code in the handler chain of your service stub on client side, it will be executed just before the XML deserialization of the server response:

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);


来源:https://stackoverflow.com/questions/37527613/changing-xml-structure-in-soap-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!