{"id":39,"date":"2008-01-18T06:25:48","date_gmt":"2008-01-18T13:25:48","guid":{"rendered":"http:\/\/www.hamsterinawheel.ca\/?p=39"},"modified":"2008-01-18T06:34:18","modified_gmt":"2008-01-18T13:34:18","slug":"handling-xsany-with-xml-beans","status":"publish","type":"post","link":"http:\/\/www.hamsterinawheel.ca\/?p=39","title":{"rendered":"Handling &lt;xs:any&gt; with XML Beans"},"content":{"rendered":"<p>This article explains how to add an XmlObject to another XmlObject.  In this case, the parent XmlObject has children of type &lt;xs:any&gt;.  The tricky part is that XML Beans does not create accessor methods for<br \/>\n<code>&lt;xs:any&gt;<\/code> elements.<\/p>\n<p>In my case, I was creating a SOAP response. Here&#8217;s the snippet that creates a SOAP fault:<\/p>\n<pre>\/\/ SOAP schema XML Bean imports\r\nimport org.xmlsoap.schemas.soap.envelope.Body;\r\nimport org.xmlsoap.schemas.soap.envelope.Detail;\r\nimport org.xmlsoap.schemas.soap.envelope.Envelope;\r\nimport org.xmlsoap.schemas.soap.envelope.EnvelopeDocument;\r\nimport org.xmlsoap.schemas.soap.envelope.Fault;\r\nimport org.xmlsoap.schemas.soap.envelope.FaultDocument;\r\nimport org.xmlsoap.schemas.soap.envelope.Header;\r\n\r\n\/\/ DOM Imports\r\nimport org.w3c.dom.Element;\r\nimport org.w3c.dom.Node;... now the real code\r\n\r\n\/\/ the XML document looks like this:\r\n    &lt;envelope&gt;\r\n        &lt;body&gt;\r\n            &lt;fault&gt;\r\n                ...\r\n            &lt;\/fault&gt;\r\n        &lt;\/body&gt;\r\n    &lt;envelope&gt;\r\n\r\n\/\/ create envelope and body\r\nEnvelopeDocument responseEnv = EnvelopeDocument.Factory.newInstance();\r\nBody responseBody = responseEnv.addNewEnvelope().addNewBody();\r\n\r\n\/\/ create fault, must be a FaultTDocument. If you just create a\r\n\/\/ Fault instance, it will be an an XML fragment, nothing will work\r\nFaultDocument faultDoc = FaultDocument.Factory.newInstance();\r\nFault fault = faultDoc.addNewFault();\r\nfault.setFaultcode(new QName(\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\", \"Client\"));\r\n\r\n... some more code that populates the fault object\r\n\r\n\/\/ now the tricky part\r\nNode bodyNode = responseBody.getDomNode();\r\nimportedNode = bodyNode.getOwnerDocument().importNode(faultDoc.getFault().getDomNode(), true);\r\nbodyNode.appendChild(importedNode);<\/pre>\n<p>The tricky part is the last 3 lines of code, I used the DOM API, not the XML Beans API, to add an XmlObject instance as a child of another XmlObject instance.  Also, make sure that the node you import comes from an element that is part of a document, not just a parent-less XmlObject instance (it would be one of those useless XML fragments, frankly that part of the API is counterintuitive.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article explains how to add an XmlObject to another XmlObject. In this case, the parent XmlObject has children of type &lt;xs:any&gt;. The tricky part is that XML Beans does not create accessor methods for &lt;xs:any&gt; elements. In my case, I was creating a SOAP response. Here&#8217;s the snippet that creates a SOAP fault: \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":0,"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hamsterinawheel.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}