Advertisement
Guest User

ZipWSTest

a guest
Apr 16th, 2013
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. import com.webservicemart.ws.*;
  2. import java.io.*;
  3. import javax.xml.parsers.*;
  4. import org.w3c.dom.*;
  5. import org.xml.sax.*;
  6. public class ZipWSTest {
  7.     public static void main(String[] args) {
  8.         USZip usZip = new USZip();
  9.         USZipSoap usZipSoap = usZip.getUSZipSoap();
  10.         String response = usZipSoap.validateZip("08863");
  11.        
  12.         response = "<?xml version='1.0'?><root>"+response+"</root>";
  13.        
  14.         try {
  15.             Document doc = XMLUtility.stringToDom(response);
  16.             NodeList nl = doc.getElementsByTagName("item");
  17.             Node n = nl.item(0);
  18.  
  19.             NamedNodeMap nnmap = n.getAttributes();
  20.        
  21.             String state = nnmap.getNamedItem("state").getNodeValue();
  22.             String lat = nnmap.getNamedItem("latitude").getNodeValue();
  23.             String lng = nnmap.getNamedItem("longitude").getNodeValue();
  24.            
  25.             System.out.println("State:"+state);
  26.             System.out.println("Latitude:"+lat);
  27.             System.out.println("Longitude:"+lng);
  28.         }catch(Exception e){
  29.             System.out.println("Error:"+e);
  30.         }
  31.         System.out.println("XML:" + response);
  32.     }
  33.    
  34.     public static class XMLUtility {
  35.         public static Document stringToDom(String xmlSource)
  36.                 throws SAXException, ParserConfigurationException, IOException {
  37.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  38.             DocumentBuilder builder = factory.newDocumentBuilder();
  39.             return builder.parse(new InputSource(new StringReader(xmlSource)));
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement