在SOAP处理程序中更改XML结构 [英] Changing XML structure in SOAP Handler

查看:157
本文介绍了在SOAP处理程序中更改XML结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Axis 1.4,并且希望在客户端内的SOAP主体的XML中插入一个附加级别.有一个服务器响应,我可以通过客户端中的javax.xml.rpc.handler.GenericHandler的子类来获得:

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

这时,我需要一个新标签,其中包含所有现有的<centres>标签.我已经存储在cl中的所有<centres>标签的列表,但是如何将新节点添加到<projectDataReturn>标签中?以及如何将现有的<centre>标签移动到新标签?我已经尝试过

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;

但是它会产生SOAPException

But it produces a SOAPException

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

怎么了?

推荐答案

我找到了一个解决方案:

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

现在,我克隆了旧的<centres>节点:

Now I clone the old <centres> nodes:

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

现在,我将新节点插入到XML中(在旧的<centres>标记前面):

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

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

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

这篇关于在SOAP处理程序中更改XML结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆