1 package com.dotmarketing.util.ups; 2 3 import java.io.StringReader ; 4 import java.util.Vector ; 5 6 import javax.xml.parsers.DocumentBuilder ; 7 import javax.xml.parsers.DocumentBuilderFactory ; 8 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.NodeList ; 11 import org.xml.sax.InputSource ; 12 import org.xml.sax.SAXException ; 13 14 15 20 21 public class UPSResponseParser { 22 23 private static Vector <UPSResponseObject> objectList= null; 24 25 28 public static Vector getObjectShippingPrices(String uriXML) { 29 try { 30 31 InputSource is = new InputSource (new StringReader (uriXML)); 32 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 33 DocumentBuilder builder = factory.newDocumentBuilder(); 34 Document document = builder.parse( is ); 35 objectList = new Vector <UPSResponseObject>(); 36 37 NodeList list = document.getElementsByTagName("RatingServiceSelectionResponse"); 38 39 int responseLength = list.getLength(); 40 for(int i= 0; i < responseLength; i++){ 41 42 UPSResponseObject object = new UPSResponseObject(); 43 44 list = document.getElementsByTagName("ResponseStatusCode"); 45 if(list.item(i) != null && list.item(i).getFirstChild() != null) 46 object.setStatusCode(list.item(i).getFirstChild().getNodeValue()); 47 48 list = document.getElementsByTagName("ResponseStatusDescription"); 49 if(list.item(i) != null && list.item(i).getFirstChild() != null) 50 object.setStatusDescription(list.item(i).getFirstChild().getNodeValue()); 51 52 list = document.getElementsByTagName("ErrorCode"); 53 if(list.item(i) != null && list.item(i).getFirstChild() != null) 54 object.setErrorCode(list.item(i).getFirstChild().getNodeValue()); 55 56 list = document.getElementsByTagName("ErrorDescription"); 57 if(list.item(i) != null && list.item(i).getFirstChild() != null) 58 object.setErrorDescription(list.item(i).getFirstChild().getNodeValue()); 59 60 list = document.getElementsByTagName("TotalCharges"); 61 if(list.item(i) != null && list.item(i).getFirstChild() != null){ 62 63 list = document.getElementsByTagName("CurrencyCode"); 64 if(list.item(i) != null && list.item(i).getFirstChild() != null) 65 object.setCurrencyCode(list.item(i).getFirstChild().getNodeValue()); 66 67 list = document.getElementsByTagName("MonetaryValue"); 68 if(list.item(i) != null && list.item(i).getFirstChild() != null) 69 object.setTotalValue(list.item(i).getFirstChild().getNodeValue()); 70 71 72 } 73 74 objectList.add(object); 75 } 76 77 } catch (SAXException e) { 78 e.printStackTrace(); 79 } catch (Exception e) { 80 e.printStackTrace(); 81 } 82 83 return objectList; 84 } 85 86 } 87 | Popular Tags |