Wednesday, October 10, 2012

Complex Element


Complex Element

Complex Element : which element contains simple elements or other complex elements is call complex element.

We can create complex elements in two ways in xsd

1.creating complex type elements and using them
2.annoums complex element


1.creating complex type elements and using them

we can create a complex element in xsd by using "complexType" key word
as follows

we can resue the structure of the complex type (it is creating class in java)

compareing java class and complex type in xsd.










Class in java

Complex Type in xsd

Class name_of_class
{

         //statements


}


<complexType name=”name_of_complex _type”>
            <sequence/all>
  
                   //simple elements

            </sequence/all>
</complexType>

class ItemType
{
          String itemcode;
          int quantity;
}





<complexType name="itemtype">
                <sequance>
                                <element name="itemcode" type="string"/>
                                <element name="quantity" type="int"/>
                </sequance>
</complexType>


ItemType  item=new ItemType();



<element name=”item” type=”itemtype”>



Structure:

<complexType name="complex_type_name">
                <sequance/all>
                                //simple elements or complex elements
                </sequance/all>
</complexType>


Example:

complexType which contains simple elements

<complexType name="itemtype">
                <sequance>
                                <element name="itemcode" type="string"/>
                                <element name="quantity" type="int"/>
                </sequance>
</complexType>

complexType which contains complex elements

<complexType name="orderitemstype">
                <sequance>
                                <element name="quantity" type="itemtype"/>
                </sequance>
</complexType>


XML Well Form


XML Well Form
For well formed XML we follow the following rules.
Well form talks about the structure of the xml document
Rules:
·         XML file should start with prolog (optional)
·         Only one root tag is allowed
·         All the elements (tags) must be under Root Tag only
·         Every stating tag must have ending tag (closing tag)
·         The level of opening tag must be closed the same level
·         XML tags are case sensitive  i.e <Tag> and <tag> both are treated as different tags
Demo:
File Name: Demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<PURCHAGEORDER> <!- root tag opening at level 0 -->
                <ORDERITEMS> <!- opening tag at level 1 -->
                                <ITEM>  <!- opening tag at level 2 -->
                                                <ITEMCODE>NOKIA345</ITEMCODE>
                                                <QUANTITY>6</QUANTITY>
                                </ITEM><!- closing tag at level 2 -->
                </ORDERITEMS> <!- closing tag at level 1 -->
                <SHIPPINGADDRESS><!- opening tag at level 1 -->
                                <ADDR>SAN</ADDR><!- opening and closing tag at level 2 -->
                                <DORNO>77-98</DORNO><!- opening and closing tag at level 2 -->                                                                                                     <STREAT>GOPALNAGAR</STREAT><!- opening and closing tag at level 2 -->
                                <CITY>HYD</CITY><!- opening and closing tag at level 2 -->
                </SHIPPINGADDRESS><!- closing tag at level 1 -->
</PURCHAGEORDER><!- closing tag at level 0 -->

Note: above demo.xml file satisfy’s all the rules so its call well formed xml
1.     For checking is well formed or not open in the browser, if its shows the above content as it we wrote
Otherwise it shows the error where and what
2.     use the xml spy tool check whether the xml is well formed or not

SIMPLE WEB SERVICE PROVIDER SIDE APPLICATION


SIMPLE WEB SERVICE PROVIDER SIDE APPLICATION
Requirement:
                Develop a web service provider side application which, provide complete information about a book by giving a book name as input.
Web service development environment:
1.       Using Jax-rpc api si (sun implementation) 
2.       Contract as  last
3.       Synchronous reply/response message exchange pattern
4.       rpc-encoded
5.       servlet end-point
Software requirements:
1.       java 1.5.0_22and java 1.6.0 in between
2.       jax-rpc specific jar files
3.        apache- tomcat 6.x serve
4.       Jwsdp 2.0 tool
Development procedure:
Step 1:
                Create a dynamic project in eclipse as follows.
File->New->Others->Dynamic web project
Give the project name and set server as tomcat
project Name: bookproject
Create a package under project : bookproject as shown
Package name: com.bookweb.service
Step 2:
                Copy the all jax-rpc specific jar files into lib folder.
  Step 3:
                Create a SEI interface as follows
                Interface name: BookInterface
package com.bookweb.service;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface BookInterface extends Remote {
      
       float getBookPrice(String isbn) throws RemoteException;

}

Step 4:
                Implemented class name: BookImpl.java
                Provide implementation for SEI interface, which is full fill our business requirements
package com.bookweb.service;

import java.rmi.RemoteException;

public class BookImpl implements BookInterface {

       public float getBookPrice(String isbn) throws RemoteException {
              float bookprice=0.0f;
              if(isbn.equals("java"))
                     bookprice=234.56f;
              return bookprice;
       }

}


Step 5:

      
 Write a configuration file under WEB-INF folder as following tags
Configuration file name: config.xml
<?xml version="1.0" encoding="UTF-8"?>

<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
       <service name="BookInfoService"
              targetNamespace="http://bookinfoservice.org/wsdl"
              typeNamespace="http://bookinfoservice.org/types"
              packageName="com.bookweb.service.binding">
              <interface name="com.bookweb.service.BookInterface"
                     servantName="com.bookweb.service.BookImpl" />
       </service>
</configuration>

Step 5:
                Install jwsdp2.0 tool
Set path for bin as C:\Sun\jwsdp-2.0\jaxrpc\bin
Run wscompile.bat with following options and pass config.xml as input
C:\Users\sant\workspace\BookService>wscompile -keep -verbose -gen:server -d src
-cp build\classes -model model-rpc-enc.xml.gz WebContent\WEB-INF\config.xml
It will generates wsdl file and model files and saxparsers and java classes
Move the wsdl and model file into WEB-INF folder
Step 5:
                Write one more configuration file jax-rpc.xml (file name with same name only) under WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<webServices
    xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd"
    version="1.0"
    targetNamespaceBase="http://bookinfoservice.org/wsdl"
    typeNamespaceBase="http://bookinfoservice.org/types"
    urlPatternBase="/getBookPrice">

    <endpoint
        name="booksriceservice"
        displayName="Book Service"
        description="A simple web service"
        wsdl="/WEB-INF/BookInfoService.wsdll"
        interface="com.bookweb.service.BookInterface" 
        implementation="com.bookweb.service.BookImpl"
            model="/WEB-INF/model-rpc-enc.xml.gz">
           
        </endpoint>

    <endpointMapping
        endpointName="booksriceservice"
        urlPattern="/getBookPrice"/>

</webServices>

Step 6:
                Export project as .war file
Give the war as input to the wsdeploy.bat
C:\wsdeploy –o taget.war bookproject.war
It generates the jax-rpc-runtime.xml and web.xml
Copy the same file into the project WEB-INF folder
Step 7:
Deploy the project in server and run the server and provide the url;
output as follows:
Web Services
Port Name
Status
Information
booksriceservice
ACTIVE
Address:
http://localhost:8080/bookproject/getBookPrice
WSDL:
Port QName:
{http://bookinfoservice.org/wsdl}BookInterfacePort
Remote interface:
com.bookweb.service.BookInterface
Implementation class:
com.bookweb.service.BookImpl
Model: