Main BLOGGER
Google
WWW THIS BLOG
Saturday, April 22, 2006
 
Play with java SOAPElement in JAX-RPC xsd:anyType

In JAX_RPC, xsd:anyType is mapped to javax.xml.soap.SOAPElement

Here is the java function to convert an int to SOAPElement

 

    public static SOAPElement int2SOAPElement(String tagName, int value)

    {

       try{            

         SOAPFactory sf = SOAPFactory.newInstance();    

         SOAPElement e = sf.createElement(tagName);

         String xsi = "http://www.w3.org/2001/XMLSchema-instance";

         Name xsiTypeString = sf.createName("type", "xsi", xsi);

         e.addAttribute( xsiTypeString, "xsd:int" );

         e.addTextNode((new Integer(value)).toString());

         return e;      

       }catch(Exception e)

       {

          System.out.println ( "int2SOAPElement, exceptioin: "+ e.toString() );

          return null;

       }

}

 

 

For example

 

    <!-- method input: array of object -->

    <xsd:complexType name="ArrayOfObject">

      <xsd:sequence>

         <xsd:element type="xsd:anyType" name="Object" minOccurs="0" maxOccurs="unbounded" />

       </xsd:sequence>

    </xsd:complexType>

 

   <!-- invoke mobile code -->

    <xsd:element name="invokeMethod">

      <xsd:complexType>

        <xsd:sequence>

          <xsd:element type="xsd:string" name="moduleName" />

          <xsd:element type="xsd:string" name="methodName" />

          <xsd:element type="tns:ArrayOfObject" name="methodParams" />

        </xsd:sequence>

      </xsd:complexType>

    </xsd:element>

 

 

          String MODULE = "CmmCode.Math";

          String METHOD = "main_entry";

          ArrayOfObject aoo= new ArrayOfObject();

          SOAPElement e = int2SOAPElement("Object",1234);

          aoo.setObject(new SOAPElement[]{e});

          retVal = service.invokeMethod(MODULE, METHOD, aoo );   

 

 

the result xml is

 

<?xml version="1.0" encoding="UTF-8"?>

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"

     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/"       

     xmlns:ns0="http://www.cs.binghamton.edu/mcws">

   <env:Body>

       <ns0:invokeMethod>

           <moduleName>CmmCode.Math</moduleName>

           <methodName>main_entry</methodName>

           <methodParams>

                <Object xsi:type="xsd:int">1234</Object>

           </methodParams>

      </ns0:invokeMethod>

   </env:Body>

</env:Envelope>




<< Home

Powered by Blogger

Google
WWW THIS BLOG