file.xml
-----------
<?xml version="1.0" encoding="UTF-8"?>
<company>
<train id="1">
<name>yong</name>
<from>mook kim</from>
<to>mkyong</to>
<arr>100000</arr>
<dep>324</dep>
</train>
<train id="2">
<name>low</name>
<from>yin fong</from>
<to>fong fong</to>
<arr>200000</arr>
<dep>525</dep>
</train>
</company>
Train.java:
----------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.asman;
/**
*
* @author laxman
*/
public class Train {
public String id;
public String name;
public String from;
public String to;
public String arr;
public String dep;
}
-----------
<?xml version="1.0" encoding="UTF-8"?>
<company>
<train id="1">
<name>yong</name>
<from>mook kim</from>
<to>mkyong</to>
<arr>100000</arr>
<dep>324</dep>
</train>
<train id="2">
<name>low</name>
<from>yin fong</from>
<to>fong fong</to>
<arr>200000</arr>
<dep>525</dep>
</train>
</company>
Train.java:
----------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.asman;
/**
*
* @author laxman
*/
public class Train {
public String id;
public String name;
public String from;
public String to;
public String arr;
public String dep;
}
Trainservice.java
-----------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.asman;
import java.io.File;
import java.io.IOException;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* @author laxman
*/
@WebService()
public class Trainservice {
/**
* Web service operation
*/
@WebMethod(operationName = "getTraindetails")
public Train getTraindetails(@WebParam(name = "id")
String id) throws IOException {
String name="null";
String from="null";
String to="null";
String arr="null";
String dep="null";
Train train=new Train();
File fXmlFile = new File("C:\\Users\\Home\\webservicewithxml\\src\\java\\com\\asman\\file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
}
Document doc = null;
try {
doc = dBuilder.parse(fXmlFile);
} catch (SAXException ex) {
} catch (IOException ex) {
}
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("train");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("from loop...."+eElement.getAttribute("id"));
if(eElement.getAttribute("id").equals(id)){
name=getTagValue("name", eElement);
from=getTagValue("from", eElement);
to=getTagValue("to", eElement);
arr=getTagValue("arr", eElement);
dep=getTagValue("dep", eElement);
}
train.name=name;
train.from=from;
train.to=to;
train.arr=arr;
train.dep=dep;
}
}
return train;
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
ClientApplication for above webservice
---------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package webservicexmlclient;
import com.asman.IOException_Exception;
import com.asman.Train;
/**
*
* @author laxman
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException_Exception {
Train train= getTraindetails("1");
System.out.println("tain name"+train.getName());
System.out.println("train number"+train.getId());
System.out.println("arraival time"+train.getArr());
System.out.println("dep timeee"+train.getDep());
System.out.println("from station"+train.getFrom());
System.out.println("to station"+train.getTo());
// TODO code application logic here
}
private static Train getTraindetails(java.lang.String id) throws IOException_Exception {
com.asman.TrainserviceService service = new com.asman.TrainserviceService();
com.asman.Trainservice port = service.getTrainservicePort();
return port.getTraindetails(id);
}
}
No comments:
Post a Comment