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

Asked

Viewed 3,301 times

0

  • 1

    Java is not with me, but it seems to me that this is already the response file. If you download this XML just work with it. But what I say is just a comment.

2 answers

2


You’ll need to focus in two ways which I will describe soon. From what I understand you want to make a request to a URL and get and interpret the answer, which in this case is the XML you showed. Also, you want to do this in Java.

I’m starting from the assumption that you still have nothing 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 XML you will need to make a request for the URL http://www.geoplugin.net/extras/location.gp 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 make 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 above code to you. But basically, it makes a GET request to the URL by passing the necessary parameters to get the answer. The answer is read, converted to String and returned by the method.

Making the parse of XML

With the answer obtained by the previous method, we now need to interpret (parse) this answer. For this, we will use the GIFT to read and get the texts that are in XML. See below how to do this:

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 is important to know is how the XML structure works (including tag repetition) so you can develop more robust code.

Concluding...

The codes above were made only as an educational example. I didn’t care about validations, exception handling or other scenarios that XML might contain. I just made a snippet of code specifically for your problem and for you to understand how Java meets what you asked for.

Below are some links that can help in your journey:

  • Juxtaposing what I needed, thank you very much.

  • Very good that helped! :)

1

  • Even if this link is a good suggestion, this answer will not be valid if one day the link fails. Also, it’s important for the community to have content right here. It would be better to include more details in your reply; Could you do that? A summary of the content of the link would help a lot! Learn more about this subject in our Community FAQ: Answers that only contain links are good?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.