Author: Weng Yan
Time: 2003-7-24
Welcome everyone to contact me, enhydra_boy @ Tom.com
Welcome to reprint, please reserve the declaration information.
The last time I talked about how Tomcat quickly developed the web service application and gave an example. However, the passing parameters are common data types. If you want to pass complex data types, such as object types, how should you do it? This article is to discuss this problem.
My solution is to implement, the Web Service server side and the client's complex data type exchange. Of course, this String is an XML format string that implements the storage of complex data types.
So, this process is roughly like this:
1 Client sending a SOAP call request
2 The server accepts data from the database, and processed into an XML string as required.
3 The client accepts the XML string and parses the information in it.
vice versa. Speaking here, it should be said that such implementation is not difficult. Then, the problem we have to solve is a quick development plan. In Java development, use objects to store complex data types, and there is also a concept in VB, then the program written by different development tools communicates with data, What technology should be adopted to ensure the development speed and quality of the software? This is the purpose of the author.
The author believes that using XML professional design tools to design data structure, using third-party development tools to automatically generate source code for the development platform. This is a relatively fast and reliable development method.
Java and VB have corresponding automatic generation tools. Java is Sun Web Service Developer Pack, VB has Microsoft Soap Toolkit. Briefly illustrate as follows:
1 Design a Schema file with XML SPY (XML Design Tool).
2 Sun Web Service Developer Pack can automatically generate Java code according to Schema, compile into a Class file. The Java program can be implemented to directly PARSE into an XML string, and the XML string is loaded, and the object information is initialized.
3 XML Spy converts Schema into XDR files (XML Data Abstract Files), SOAP MESSAGE Object Generator in SOAP Toolkit automatically generates VB class code. Function is the same.
Let's start practice.
First step, XML Schema design
Start XML SPY (if not installed, downloaded and installed), create new schema files, all design is visualized.
Save and get an EMPLOYEEROT.XSD file.
In the second step, the Java source program is automatically generated.
First download the Sun Web Service Developer Pack 1.1, install and follow the documentation.
In the Command window, run% jaxb_home% / bin / xjc.bat employeeroot.xsd -p com.sam.util.emp. After the command is successful, the Java source file will be automatically generated.
The third step is compiled and generated and generates a CLASS code.
In the Command window, run% java_home% / bin / javac COM / SAM / UTIL / EMP / *. Java COM / SAM / UTIL / EMP / IMPL / *. JAVA.
You can run the JAR command to pack. You can also generate a technical document for your class for your class.
The fourth is established to establish a web service for testing.
In this example, the platform used by the author is Tomcat Axis. You can see http://www.9cbs.net/develop/read_article.asp?id=19782 to learn about the specific configuration. Note, please copy the JAXB and JAXP libraries to $ Tomcat_Home / Axis / Web-INF / LIBS. Main there are: jaxb-api.jar, jaxb-libs.jar, jaxb-ri.jar, jaxb-xjc.jar, jaxp-api.jar. And copy the class library package you get in the third step. In the AXIS directory, create an EmpService.jws file, the code is as follows: import java.io.fileinputstream;
Import java.io.fileoutputstream;
Import java.io.ioException;
Import java.math.bigdecimal;
Import javax.xml.bind.jaxbcontext;
Import javax.xml.bind.jaxbexception;
Import javax.xml.bind.marshaller;
Import javax.xml.bind.unmarshaller;
Import javax.xml.transform.stream. *;
Import Java.io.stringReader;
Import java.io.stringwriter;
Import com.sam.util.emp. *;
Public class empservice {
Public string geteMPloyees ()
{
StringWriter Sout = New StringWriter ();
Try {
// Create a jaxbcontext capable of handling classes generated INTO
JAXBCONText JC = jaxbcontext.newinstance ("com.sam.util.emp"); unmarshaller u = jc.createunmarshaller ();
StringBuffer Xmlstr = new stringbuffer ("
New streamsource (New StringReader)
Xmlstr.tostring ()))))) ;;
Emps.geTemPloyee (). clear ();
Employee EMP = New com.sam.util.emp.impl.employeeImpl ();
Emp.SetFirstName ("john");
Emp.SETMIDDLENAME ("");
Emp.SetlastName ("Bob");
Emp.Setssn ("555-111");
Emps.geTemPloyee (). Add (emp);
EMP = new com.sam.util.emp.impl.employeeImpl ();
Emp.SetFirstName ("Tom");
Emp.SETMIDDLENAME ("");
Emp.SetlastName ("bill");
Emp.Setssn ("567-281");
Emps.geTemPloyee (). Add (emp);
Marshaller M = jc.createmarshaller ();
M.SETPROPERTY (Marshaller.jaxb_formatted_output, boolean.true);
StreamResult Result = New StreamResult (Sout);
M.Marshal (EMPS, RESULT);
//System.out.println (sout.tostring ());
} catch (jaxbexception je) {
Je.printStackTrace ();
""; "
}
Return Sout.toString ();
}
}
Run http: // localhost: 8080 / axis / Empservice.jws? WSDL, you can see the WSDL interface corresponding to the service file.
Step 5, use the Convert DTD / Schema in XML SPY to convert Employeeroot.xsd to the XML Data file (EMPLOYEEROOT.XDR).
The converted file is as follows:
XML Version = "1.0" Encoding = "UTF-8"?>
Private subduDread_click ()
ON Error ResMe next
Dim SoapClient As Object
DIM SRESULT AS STRING
Set soapclient = creteObject ("mssoap.soapclient30")
Call SoapClient.msoapinit2 (Text1.Text, "," "" "" "" "" "" "", ""
IF Err.Number <> 0 THEN
Msgbox soapclient.faultstring, vbexclamation
EXIT SUB
END IF
'Now the client can call an operation listed in the porttype element
'specified when caling mssoapinit ().
SResult = soapclient.geTemPloyees ()
IF Err.Number <> 0 THEN
Msgbox soapclient.faultstring, vbexclamation
EXIT SUB
END IF
Dim OSoAP as mssmo.ioapMessagePart
Dim Odom as msxml2.domdocument30
DIM Oemps as EmployeesRoot.employees
DIM OEMP as EmployeesRoot.employee
'Now We Parse the XML Data Into Our Class
Set = new msxml2.domdocument30if om.loadXML (SRESULT) THEN
Set Oemps = New EmployeesRoot.employees
Set OSoAP = OEMPS
For Each Oxmlnode in in odom.childnodes
IF oxmlnode.nodetype = node_element then
IF _
Oxmlnode.basename = osoap.elementname and _
Oxmlnode.namespaceuri = osoap.namespaceuri _
THEN
Set osoap.element = oxmlnode
EXIT for
END IF
END IF
NEXT
List1.clear
For Each Oemp in Oemps.Employee
List1.additem Oemp.ssn & Vbtab & Oemp.Firstname & "," & Oemp.middlename & "" & Oemp.Firstname
NEXT
END IF
End Sub
Run, the test results are as follows: Summary, through this example, the author believes that it has brought us a new concept and ideas. Using XML to design the design of the data structure, the code is automatically obtained based on the design mode. This development method makes it possible to concentrate concentrate on data models and business logic, and the maintenance cost of code is also greatly reduced.