get values from soap response in java

6.6k views Asked by At

I am calling a web-service method through a web-service client generated by the netbeans IDE.

 private String getCitiesByCountry(java.lang.String countryName) {
        webService.GlobalWeatherSoap port = service.getGlobalWeatherSoap();
        return port.getCitiesByCountry(countryName);
    }

So i call this method inside my program,

String b = getWeather("Katunayake", "Sri Lanka"); 

and it will give me a string output which contains xml data.

String b = getWeather("Katunayake", "Sri Lanka"); = (java.lang.String) <?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
  <Location>Katunayake, Sri Lanka (VCBI) 07-10N 079-53E 8M</Location>
  <Time>Jun 22, 2015 - 06:10 AM EDT / 2015.06.22 1010 UTC</Time>
  <Wind> from the SW (220 degrees) at 10 MPH (9 KT):0</Wind>
  <Visibility> greater than 7 mile(s):0</Visibility>
  <SkyConditions> partly cloudy</SkyConditions>
  <Temperature> 86 F (30 C)</Temperature>
  <DewPoint> 77 F (25 C)</DewPoint>
  <RelativeHumidity> 74%</RelativeHumidity>
  <Pressure> 29.74 in. Hg (1007 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>

How may i get the value of <Location>,<SkyConditions>,<Temperature>.

2

There are 2 answers

0
Sridhar On BEST ANSWER

You can go for XPath if you need only these 3 values. Otherwise, DOM reads the entire document. It is very easy to write XPath expressions those directly fetch the node to read values.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
    builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    e.printStackTrace();  
}
String xml = ...; // <-- The XML SOAP response
Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes()));
XPath xPath =  XPathFactory.newInstance().newXPath();
String location = xPath.compile("/CurrentWeather/Location").evaluate(xmlDocument);
String skyCond = xPath.compile("/CurrentWeather/SkyConditions").evaluate(xmlDocument);
String tmp = xPath.compile("/CurrentWeather/Temperature").evaluate(xmlDocument);

If, you need to fetch many XML nodes and frequently, then go for DOM.

1
huynhenberg On

One way is using a DOM parser, using http://examples.javacodegeeks.com/core-java/xml/java-xml-parser-tutorial as a guide:

String b = getWeather("Katunayake", "Sri Lanka"); 
InputStream weatherAsStream = new ByteArrayInputStream(b.getBytes(StandardCharsets.UTF_8));

DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
org.w3c.dom.Document weatherDoc = builder.parse(weatherAsStream);

String location = weatherDoc.getElementsByTagName("Location").item(0).getTextContent();
String skyConditions = weatherDoc.getElementsByTagName("SkyConditions").item(0).getTextContent();
String temperature = weatherDoc.getElementsByTagName("Temperature").item(0).getTextContent();

This has no exception handling and might break if there are more than one elements with the same name, but you should be able to work from here.