我使用 Axis2 框架和 XMLBeans 作为数据绑定方法生成了一个客户端。
XSD 架构如下:
<代码>
生成的作为 SOAP 请求一部分的 java 对象包含允许获取和设置配置文件的 getter 和 setter 方法。这是方法签名:
requestDocument.setProfile(XmlObject profile);
问题是,即使我必须传递多个节点作为配置文件而不是有效的 XML 文档,但 XMLObject 需要一个带有根节点的 XML 文档。
我需要通过:
<账户>
实际上,我使用的服务需要这些节点,但并未将它们限制在架构中。因此,我无法添加另一个根节点,因为即使服务不会抛出任何异常,配置文件也将无法使用。
XMLBeans 已经添加了底层 XML 树,我的意思是请求文档中的配置文件节点。因此,我不能将它用作根节点。如果我添加根节点,将创建以下 XML
<代码><个人资料>
<个人资料>
我希望文档的格式如下:
<个人资料>
<帐户>
<收款人>
我不想修改服务的架构。我想知道 Axis2/XMLBeans 是否有办法解决这个问题。
I have generated a client using Axis2 framework with XMLBeans as the data binding method.
The XSD schema is the following:
<xsd:schema>
<xsd:element name="profile" type="anyType"/>
</xsd:schema>
The java object generated which takes part of the SOAP request contains getter and setter methods that allow to get and set the profile. Here is the method signature:
requestDocument.setProfile(XmlObject profile);
The problem is that even if that I have to pass several nodes as the profile and not a valid XML document, but XMLObject expects a XML document with a root node.
I need to pass:
<accounts></accounts>
<payees></payees>
Actually, the service I use expects those nodes but did not constrains them in the schema. Thus, I can't add another root node because even if the service won't throw any exceptions, the profile won't be usable.
XMLBeans already adds the underlying XML tree, I mean the profile node in the request document. Thus, I can't use it as a root node. if I add a root node, the following XML will be created
<profile>
<profile></profile>
</profile>
And I want the document be formatted as follow:
<profile>
<accounts></accounts>
<payees></payees>
</profile>
I prefer not modifying the schema of the service. I would want to know if there is a way with Axis2/XMLBeans to tackle this issue.
发布评论
评论(1)
我找到了一个可能是解决方法的解决方案,而不是应该完成的干净方式。
而不是使用
XMLObject
设置配置文件,如下所示:requestDocument.setProfile(XmlObject 配置文件);
我使用 org.w3c.dom.Node 对象来创建配置文件内容。见下文:
1. 创建要添加到配置文件中的元素
org.w3c.dom.Element
:Element accountElt = profileDocument.createElement("accounts");
Element payeesElt = profileDocument.createElement("payees");
2. 在要发送到服务的文档中创建一个空配置文件,请注意该对象是自动生成的:
requestDocument.addNewProfile();
3. 获取空配置文件并将子节点添加到其根节点:
requestDocument.getProfile().getDomNode().appendChild(accountsElt);
requestDocument.getProfile().getDomNode().appendChild(payeesElt);
我希望它有帮助。
I find a solution that is probably a workaround and not the clean way it should be done.
Instead of setting the profile with an
XMLObject
as follow:requestDocument.setProfile(XmlObject profile);
I used
org.w3c.dom.Node
object to create the profile content. See below:1. Create the elements
org.w3c.dom.Element
to be added to the profile:Element accountsElt = profileDocument.createElement("accounts");
Element payeesElt = profileDocument.createElement("payees");
2. Create an empty profile in the document to be send to the service, note that the object is auto generated:
requestDocument.addNewProfile();
3. Get the empty profile and add children to its root node:
requestDocument.getProfile().getDomNode().appendChild(accountsElt);
requestDocument.getProfile().getDomNode().appendChild(payeesElt);
I hope it helps.