Hi,

Jay SenSharma

Jay SenSharma

XMLBeans is a tool that allows access to the full power of XML in a Java friendly way. The idea is to take advantage of the richness and features of XML and XML Schema and have these features mapped as naturally as possible to the equivalent Java language and typing constructs. XMLBeans uses XML Schema to compile Java interfaces and classes that can then be used to access and modify XML instance data. Using XMLBeans is similar to using any other Java interface/class: with methods like getFoo or setFoo, just as when working with Java. While a major use of XMLBeans is to access XML instance data with strongly typed Java classes there are also APIs that allow access to the full XML infoset (XMLBeans keeps XML Infoset fidelity) as well as to allow reflection into the XML schema itself through an XML Schema Object model.

Objective
A major objective of XMLBeans has been to be applicable in all non-streaming (in memory) XML programming situations. The developer should be able to compile their XML Schema into a set of Java classes and know that they will be able to:
1). use XMLBeans for all of the schemas they encounter.
2). access the XML at whatever level is necessary without other tools.
———————————————–
Here we are going to Utilize XMLBeans in WebLogic WebService.

Step1). Develope the XSD file like: Place it in the “src” directory Example: D:TestJavaXSD_ServicesrcCustomer.xsd
“Customer.xsd”
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:customer=”http://www.mywebsite.com/customer”
xmlns:flight=”http://www.mywebsite.com/flight”
targetNamespace=”http://www.mywebsite.com/customer”>
<xsd:annotation>
<xsd:documentation>Test Schema</xsd:documentation>
</xsd:annotation>
<xsd:element name=”customer” type=”customer:Customer”/>
<xsd:complexType name=”Customer”>
<xsd:sequence>
<xsd:element name=”FlightNumber” type=”xsd:positiveInteger”/>
<xsd:element name=”departAirport” type=”xsd:string”/>
<xsd:element name=”departDate” type=”xsd:date”/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
————————
Step2). Develope the WebService like :   Place it in the “src” directory. Example: D:TestJavaXSD_ServicesrcHelloWorldImpl.java
“HelloWorldImpl.java”
package examples.webservices.hello_world;
import javax.jws.*;
import com.mywebsite.customer.*;
import com.mywebsite.customer.impl.*;
import javax.jws.soap.SOAPBinding;
@WebService(name=”HelloWorldPortType”, serviceName=”HelloWorldService”)
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL,parameterStyle=SOAPBinding.ParameterStyle.BARE)
public class HelloWorldImpl
{
@WebMethod
public String sayHelloWorld(String message)
{
System.out.println(“sayHelloWorld:” + message);
return “Here is the message: ‘” + message + “‘”;
}
@WebMethod
public String sayHelloCustomer(CustomerDocument custDoc)
{
System.out.println(“nnt custDoc: ” + custDoc);
return ” custDoc: ‘” + custDoc + “‘”;
}
}
———————–
Step3). Better Copy and paste the “com.bea.core.xml.beaxmlbeans_2.2.0.0.jar” XMLBean Jar file also in the “src” directory. Example: D:TestJavaXSD_Servicesrccom.bea.core.xml.beaxmlbeans_2.2.0.0.jar
—————————–
Step4). No we need to provide the Ant Build sothat we can compile the XSD and our WebService…Example: D:TestJavaXSD_Servicebuild.xml
“build.xml”
<project name=”webservices-hello_world” default=”all”>
<!– set global properties for this build –>
<property name=”wls.username” value=”weblogic” />
<property name=”wls.password” value=”weblogic” />
<property name=”wls.hostname” value=”localhost” />
<property name=”wls.port” value=”7001″ />
<property name=”wls.server.name” value=”AdminServer” />
<property name=”ear.deployed.name” value=”helloWorldEar” />
<property name=”example-output” value=”output” />
<property name=”ear-dir” value=”${example-output}/helloWorldEar” />
<property name=”clientclass-dir” value=”${example-output}/clientclasses” />
<property name=”src” value=”src” />
<path id=”client.class.path”>
<pathelement path=”${clientclass-dir}”/>
<pathelement path=”${java.class.path}”/>
</path>
<taskdef name=”jwsc” classname=”weblogic.wsee.tools.anttasks.JwscTask”  />
<taskdef name=”clientgen”  classname=”weblogic.wsee.tools.anttasks.ClientGenTask” />
<taskdef name=”wldeploy” classname=”weblogic.ant.taskdefs.management.WLDeploy”/>
<taskdef name=”xmlbean” classname=”org.apache.xmlbeans.impl.tool.XMLBean” classpath=”src/com.bea.core.xml.beaxmlbeans_2.2.0.0.jar;${java.class.path}”/>
<target name=”all” depends=”clean,build-service,deploy,client” />
<target name=”clean” depends=”undeploy”>
<delete dir=”${example-output}”/>
</target>
<target name=”xmlbeans”>
<xmlbean schema=”src/Customer.xsd” download=”true” destfile=”src/customers.jar”/>
</target>
<target name=”build-service” depends=”xmlbeans”>
<jwsc srcdir=”src” destdir=”${ear-dir}” classpath=”src/com.bea.core.xml.beaxmlbeans_2.2.0.0.jar;${java.class.path};src/customers.jar”>
<jws file=”HelloWorldImpl.java” explode=”true”/>
</jwsc>
<mkdir dir=”${ear-dir}/HelloWorldImpl/WEB-INF/lib” />
<copy todir=”${ear-dir}/HelloWorldImpl/WEB-INF/lib”>
<fileset dir=”${src}”>
<include name=”customers.jar”/>
</fileset>
</copy>
</target>
<target name=”deploy”>
<wldeploy action=”deploy”  name=”${ear.deployed.name}” source=”${ear-dir}” user=”${wls.username}” password=”${wls.password}” verbose=”true” adminurl=”t3://${wls.hostname}:${wls.port}” targets=”${wls.server.name}” />
</target>
<target name=”undeploy”>
<wldeploy action=”undeploy” name=”${ear.deployed.name}” failonerror=”false” user=”${wls.username}” password=”${wls.password}” verbose=”true” adminurl=”t3://${wls.hostname}:${wls.port}” targets=”${wls.server.name}” />
</target>
<target name=”client”>
<clientgen
wsdl=”http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldImpl?WSDL”
destDir=”${clientclass-dir}”
packageName=”client”/>
<javac
srcdir=”${clientclass-dir}” destdir=”${clientclass-dir}”
includes=”**/*.java”/>
<javac
srcdir=”src” destdir=”${clientclass-dir}”
includes=”client/**/*.java”/>
</target>
<target name=”run”>
<java classname=”client.Main”
fork=”true” failonerror=”true” >
<classpath refid=”client.class.path”/>
<arg line=”http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldImpl” />
</java>
</target>
</project>
——————————
Step5). Now open a command prompt and then run the “setWLSEnv.cmd” or “setDomainEnv.cmd” then run the ANT file like:
D:TestJavaXSD_Service> ant
———————
Thanks & Regards
Jay SenSharma
If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.