Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Web Services

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

WWW.VIDYARTHIPLUS.

COM

EX.NO: PARSING XML DOCUMENT USING SAX PARSER

DATE:

AIM:

To write a program to parse xml document using sax parser.

PROCEDURE:

STEP 1: Start the process

STEP 2: Open the NetBeans IDE 8.0.2 by double clicking on it.

STEP 3: Create the new application by click File->New project->java

STEP 4: Right click the application choose New->xml document to create the xml.

STEP 5: Then double click the .java file in the application.

STEP 6: Write the java code and save it.

STEP 7: Save the whole project and run it.

STEP 8: Verify the result.

STEP 9: Stop the process


WWW.VIDYARTHIPLUS.COM

CODING:

XML:

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

<emp-details>

<staff1>

<staffname>Gowri.s</staffname>

<staffid>001</staffid>

<staffdesig>HR</staffdesig>

<staffsal>50000</staffsal>

</staff1>

<staff2>

<staffname>Priya.N</staffname>

<staffid>002</staffid>

<staffdesig>MANAGER</staffdesig>

<staffsal>30000</staffsal>

</staff2>

<staff3>

<staffname>Saranya.k</staffname>

<staffid>003</staffid>

<staffdesig>HR</staffdesig>

<staffsal>50000</staffsal>

</staff3>

<staff4>

<staffname>Anitha.M</staffname>

<staffid>005</staffid>
WWW.VIDYARTHIPLUS.COM

<staffdesig>ACCOUNTANT</staffdesig>

<staffsal>40000</staffsal>

</staff4>

</emp-details>

JAVA:

package parser;

import java.security.KeyStore.Entry.Attribute;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class Parser

public static void main(String[] args)

try

SAXParserFactory factory=SAXParserFactory.newInstance();

SAXParser saxparser=factory.newSAXParser();

DefaultHandler handler=new DefaultHandler()

boolean bfname=false;

boolean blname=false;

boolean bnname=false;
WWW.VIDYARTHIPLUS.COM

boolean bsalary=false;

public void startElement(String uri,String localName,String qName,Attributes attributes)

throws SAXException

System.out.println("startElement:"+qName);

if(qName.equalsIgnoreCase("staffname"))

bfname=true;

if(qName.equalsIgnoreCase("staffid"))

blname=true;

if(qName.equalsIgnoreCase("staffdesig"))

bnname=true;

if(qName.equalsIgnoreCase("staffsal"))

bsalary=true;

public void endElement(String uri,String localName,String qName)throws SAXException

System.out.println("endelement:"+qName);
WWW.VIDYARTHIPLUS.COM

public void characters(char ch[],int start,int length)throws SAXException

if(bfname)

System.out.println("staffname:"+new String(ch,start,length));

bfname=false;

if(blname)

System.out.println("staffid:"+new String(ch,start,length));

blname=false;

if(bnname)

System.out.println("staffdesig:"+new String(ch,start,length));

bnname=false;

if(bsalary)

System.out.println("staffsalary:"+new String(ch,start,length));

bsalary=false;

};
WWW.VIDYARTHIPLUS.COM

saxparser.parse("C:\\DocumentsandSettings\\PG-Student\\My

Documents\\NetBeansProjects\\parser\\newXMLDocument.xml",handler)
}

catch(Exception e)

e.printStackTrace();

}
WWW.VIDYARTHIPLUS.COM

OUTPUT:

run:

startElement:emp-details

startElement:staff1

startElement:staffname

staffname:Gowri.s

endelement:staffname

startElement:staffid

staffid:001

endelement:staffid

startElement:staffdesig

staffdesig:HR

endelement:staffdesig

startElement:staffsal

staffsalary:50000

endelement:staffsal

endelement:staff1

startElement:staff2

startElement:staffname

staffname:Priya.N

endelement:staffname

startElement:staffid

staffid:002

endelement:staffid

startElement:staffdesig
WWW.VIDYARTHIPLUS.COM

staffdesig:MANAGER

endelement:staffdesig

startElement:staffsal

staffsalary:30000

endelement:staffsal

endelement:staff2

startElement:staff3

startElement:staffname

staffname:Saranya.k

endelement:staffname

startElement:staffid

staffid:003

endelement:staffid

startElement:staffdesig

staffdesig:HR

endelement:staffdesig

startElement:staffsal

staffsalary:50000

endelement:staffsal

endelement:staff3

startElement:staff4

startElement:staffname

staffname:Anitha.M

endelement:staffname

startElement:staffid
WWW.VIDYARTHIPLUS.COM

staffid:004

endelement:staffid

startElement:staffdesig

staffdesig:ACCOUNTANT

endelement:staffdesig

startElement:staffsal

staffsalary:40000

endelement:staffsal

endelement:staff4

endelement:emp-details

BUILD SUCCESSFUL (total time: 0 seconds)

RESULT:

Thus the above parsing program has been executed successfully.


WWW.VIDYARTHIPLUS.COM

EX.NO: WEB SERVICE CREATION

DATE:

AIM:

To write a program to perform add operation using NetBeans IDE.

PROCEDURE:

STEP 1: Start the process

STEP 2: Open the NetBeans IDE 8.0.2 by double clicking on it.

STEP 3: Create the new application by clicking File->New project->javaweb(web application).

STEP 4: Design the application by double clicking index.jsp file.

TO CREATE WEB SERVICE:

STEP 1: Right click on project name, click New->Web Service. A window will appear. In that

check the implement web service as stateless session bean and click finish.

STEP 2: To test a web service .go to project tab. Under web service folder. Select our web

service name and right click choose->Test Web Service.

STEP 3: To add parameters click Add button in the Add operation window and click ok.

STEP 4: Save the project and run.

STEP 5: Stop the process


WWW.VIDYARTHIPLUS.COM

CODING:

package add1;

import javax.jws.WebService;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.ejb.Stateless;

@WebService(serviceName = "add")

@Stateless()

public class add {

@WebMethod(operationName = "hello")

public String hello(@WebParam(name = "name") String txt) {

return "Hello " + txt + " !";

@WebMethod(operationName = "add")

public int add(@WebParam(name = "i") final int i, @WebParam(name = "j") final int j) {

int k=i+j;

return k;

}
WWW.VIDYARTHIPLUS.COM

OUTPUT:

Add Method invocation

Method parameter(s)

Type Value
int 5
int 5

Method returned
int : "10"

SOAP Request

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


xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:add xmlns:ns2="http://add1/">
<i>5</i>
<j>5</j>
</ns2:add>
</S:Body>
</S:Envelope>

SOAP Response

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


xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:addResponse xmlns:ns2="http://add1/">
<return>10</return>
</ns2:addResponse>
</S:Body>
</S:Envelope>

RESULT:

Thus the above web service program has been executed successfully.
WWW.VIDYARTHIPLUS.COM

EX.NO: CURRENCY CONVERSION

DATE:

AIM:

To write a program to perform currency conversion using NetBeans IDE.

PROCEDURE:

STEP 1: Start the process

STEP 2: Open the NetBeans IDE 8.0.2 by double clicking on it.

STEP 3: Create the new application by clicking File->New project->javaweb(web application).

STEP 4: Design the application by double clicking index.jsp file.

TO CREATE WEB SERVICE:

STEP 1: Right click on project name, click New->Web Service. A window will appear. In that

check the implement web service as stateless session bean and click finish.

STEP 2: To test a web service .go to project tab. Under web service folder. Select our web

service name and right click choose->Test Web Service.

STEP 3: To add parameters click Add button in the Add operation window and click ok.

STEP 4: Save the project and run.

STEP 5: Stop the process


WWW.VIDYARTHIPLUS.COM

CODING:

package conversion;

import javax.jws.WebService;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.ejb.Stateless;

@WebService(serviceName = "currency1")

@Stateless()

public class currency1 {

@WebMethod(operationName = "hello")

public String hello(@WebParam(name = "name") String txt) {

return "Hello " + txt + " !"; }

@WebMethod(operationName = "rupee_to_euro")

public double rupee_to_euro(@WebParam(name = "rupee") final double rupee ) {

double euro = 66;

double rate = rupee * euro;

return rate;

@WebMethod(operationName = "euro_to_rupee")

public double euro_to_rupee(@WebParam(name = "euro") final float euro) {

int rupee=1;

float rate=euro * rupee;

return (int)rate;

}
WWW.VIDYARTHIPLUS.COM

OUTPUT:

rupeeToEuro Method invocation

Method parameter(s)

Type Value
double 23

Method returned

double : "1518.0"

SOAP Request

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


xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:rupee_to_euro xmlns:ns2="http://conversion/">
<rupee>23.0</rupee>
</ns2:rupee_to_euro>
</S:Body>
</S:Envelope>

SOAP Response

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


xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:rupee_to_euroResponse xmlns:ns2="http://conversion/">
<return>1518.0</return>
</ns2:rupee_to_euroResponse>
</S:Body>
</S:Envelope>
WWW.VIDYARTHIPLUS.COM

euroToRupee Method invocation

Method parameter(s)

Type Value
float 23.6776

Method returned

double : "23.0"

SOAP Request

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


xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:euro_to_rupee xmlns:ns2="http://conversion/">
<euro>23.6776</euro>
</ns2:euro_to_rupee>
</S:Body>
</S:Envelope>

SOAP Response

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


xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:euro_to_rupeeResponse xmlns:ns2="http://conversion/">
<return>23.0</return>
</ns2:euro_to_rupeeResponse>
</S:Body>
</S:Envelope>

RESULT:

Thus the above currency conversion program has been executed successfully.

You might also like