How to "capture" Webservice (XML) data in Java SE?

0

I am a layman in the subject webservice and I need to capture the data of a webservice that returns a little information.

The webservice is this: link

I found a lot of information on the internet but only for Java EE and in very complex ways.

    
asked by anonymous 24.03.2014 / 02:50

2 answers

2

You will need to focus on two things that I will describe shortly. From what I understand you want to make a request for a URL and get and interpret the response , which in this case is the XML you have displayed. Also, you want to do this in Java.

I'm assuming that you do not have anything yet to do this task , so I'm going to split the answer into two parts: Requesting the URL and Reading and interpreting the response XML.

Making a request in Java

To get the XML you will need to request the URL link by passing three parameters via GET . The parameters are:

  • lat;
  • long;
  • format;

Passing the parameters and making the request, you will get the answer. The code snippet below shows how to do the request in Java:

private String fazRequest() throws IOException {
    String latitude = "-27.8161100";
    String longitude = "-50.3261100";
    String format = "xml";
    URL url = new URL("http://www.geoplugin.net/extras/location.gp");

    StringBuilder urlParametros = new StringBuilder();
    urlParametros.append("lat=").append(latitude);
    urlParametros.append("&long=").append(longitude);
    urlParametros.append("&format=").append(format);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(urlParametros.toString());
    wr.flush();
    wr.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuilder response = new StringBuilder();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    return response.toString();
}

I leave the task of understanding all the code above for you. But basically, it does a request GET for the URL passing the parameters needed to get the response. The response is read, converted to String, and returned by the method.

parse of the XML

With the answer obtained by the previous method, we now need to interpret (parse) this answer. To do so, we'll use DOM to read and get the texts that are in XML. Here's how to do it:

private void parseXML(String xml) throws ParserConfigurationException, SAXException, IOException {
    InputSource is = new InputSource(new StringReader(xml));
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);

    doc.getDocumentElement().normalize();
    String lugar = doc.getElementsByTagName("geoplugin_place").item(0).getTextContent();
    System.out.println(lugar);

    String regiao = doc.getElementsByTagName("geoplugin_region").item(0).getTextContent();
    System.out.println(regiao);

    String pais = doc.getElementsByTagName("geoplugin_countryCode").item(0).getTextContent();
    System.out.println(pais);
}

I also leave the code above for you to study its details. What's important to know is how the XML structure works (even with tag repetition) so you can develop a more robust code.

Concluding ...

The above codes were made just as a didactic example . Do not worry about validations, exception handling or other scenarios that XML may contain. I just did a code snippet specifically for your problem and for you to understand how Java meets what you asked for.

Below are some links that can help with your journey:

24.03.2014 / 15:19
1

Daew Giovani Raci Paganini checks the consumption of the webservice explained by caelum also this netbeans Client 1: Java Class in Java SE Application . Good learning !!!

    
24.03.2014 / 04:33