Blame | Last modification | View Log | RSS feed
package in.shop2020.serving.services;import in.shop2020.logistics.ShipmentUpdate;import in.shop2020.thrift.clients.LogisticsServiceClient;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.apache.thrift.TException;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;public class AramaxTrackingService {private String awbNumber;private InputSource inputSource;private XPath xpath;public static String ARAMAX_URL = "http://www.aramex.com/track_xml.asp?ShipmentNumber=";public AramaxTrackingService(String awbNumber){this.awbNumber = awbNumber;String uri = ARAMAX_URL + this.awbNumber;this.inputSource = new InputSource(uri);this.xpath= XPathFactory.newInstance().newXPath();}public String getAwbNumber() {return awbNumber;}public void setAwbNumber(String awbNumber) {this.awbNumber = awbNumber;}public InputSource getInputSource() {return inputSource;}public void setInputSource(InputSource inputSource) {this.inputSource = inputSource;}public String getUpdates(){String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";NodeList nodes = null;try {nodes = (NodeList) xpath.evaluate(expression, this.inputSource, XPathConstants.NODESET);}catch(XPathExpressionException xpee) {return null;}for(int i=nodes.getLength()-1; i>=0; i--) {Node node = nodes.item(i);NodeList childNodes = node.getChildNodes();System.out.println("Action date is : " + childNodes.item(0).getTextContent());System.out.println("PIN number is : " + childNodes.item(1).getTextContent());System.out.println("Entry Date is : " + childNodes.item(3).getTextContent());System.out.println("Update City is : " + childNodes.item(6).getTextContent());System.out.println("Tracking Code Desc is :" + childNodes.item(7).getTextContent());System.out.println("UpdateLocation is :" + childNodes.item(8).getTextContent());System.out.println("UpdateCountry is :" + childNodes.item(11).getTextContent());System.out.println("Tracking Condition is :" + childNodes.item(12).getTextContent());System.out.println("Customer Desc is :" + childNodes.item(14).getTextContent());System.out.println("\n\n\n\n ");ShipmentUpdate update = new ShipmentUpdate();update.setDescription(childNodes.item(14).getTextContent());update.setPin(childNodes.item(1).getTextContent());update.setTimestamp(10);try {LogisticsServiceClient lsc = new LogisticsServiceClient();lsc.getClient().updateShipmentStatus(this.awbNumber, update);} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}//childNodes.item(0).getTextContent()/*for(int j=0; j<childNodes.getLength(); j++) {System.out.println("value is :" +j + " " + childNodes.item(j).getTextContent());}*/ catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("nodes length " + nodes.getLength() );return nodes.toString();}public String getOriginCity(){String expression = "/HAWBs/HAWBDetails/HAWBOriginEntity";NodeList nodes = null;try {nodes = (NodeList) xpath.evaluate(expression, this.inputSource, XPathConstants.NODESET);}catch(XPathExpressionException xpee) {return null;}System.out.println("nodes length " + nodes.getLength() + nodes.item(0).getTextContent());return nodes.toString();}public static void main(String[] args){System.out.println("Aramax number is " + "6149183666");AramaxTrackingService trackingService = new AramaxTrackingService("6149183666");System.out.println("Document is " + trackingService.getInputSource());System.out.println("origin city is : " + trackingService.getOriginCity());trackingService.getUpdates();}}