Axis2 with XMLBeans data binding - Problem with a java client generated from a XSD schema with anyType
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>开发者_JAVA百科;</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.
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.
精彩评论