import org.soapinterop.types.MyElement;
import org.soapinterop.types.SOAPStruct;
 
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
 
import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
import org.apache.axiom.om.OMElement;
 
/**
 * Demo class that uses classes generated by the Axis2 XSD2Java tool.
 *
 * The <code>MyElement</code> and <code>SOAPStruct</code> classes are created running
 * the command <code>java org.apache.axis2.schema.XSD2Java schema.xsd outdir</code>
 * using the following schema:
 *
 * <schema xmlns="http://www.w3.org/2001/XMLSchema"
 *         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 *         xmlns:tns="http://soapinterop.org/types"
 *         targetNamespace="http://soapinterop.org/types">
 *    <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
 *    <complexType name="SOAPStruct">
 *       <sequence>
 *          <element name="varString" type="xsd:string"/>
 *          <element name="varInt" type="xsd:int"/>
 *          <element name="varFloat" type="xsd:float"/>
 *       </sequence>
 *    </complexType>
 *    <element name="myElement" type="tns:SOAPStruct"/>
 * </schema>
 */
public class AxisXSD2JavaDemo {
   static String xmlString = "<ns:myElement xmlns:ns=\"http://soapinterop.org/types\">" + 
                             "   <varString>Hello</varString>" + 
                             "   <varInt>5</varInt>" + 
                             "   <varFloat>3.3</varFloat>" + 
                             "</ns:myElement>";
 
   public static void main(String[] args) throws Exception {
      /////////////////////////////////////////////////////
      // Message parsing                                 //
      /////////////////////////////////////////////////////
      XMLStreamReader reader =
         XMLInputFactory.newInstance().createXMLStreamReader(
            new java.io.ByteArrayInputStream(xmlString.getBytes()));
 
      MyElement elt = MyElement.Factory.parse(reader);
      SOAPStruct struct = elt.getMyElement();
 
      // Print the value of one of the elements
      System.out.println(struct.getVarString());
 
      /////////////////////////////////////////////////////
      // Message construction                            //
      /////////////////////////////////////////////////////
      MyElement myElement = new MyElement();
      SOAPStruct soapStruct = new SOAPStruct();
      soapStruct.setVarString("Bye");
      soapStruct.setVarFloat(33.3F);
      soapStruct.setVarInt(6);
      myElement.setMyElement(soapStruct);
 
      OMDOMFactory factory = new OMDOMFactory();
      OMElement element = myElement.getOMElement(null, factory);
 
      // Print the generated message
      System.out.println(element.toString());
   }
}
 
Powered by GeSHi