Collecting Data from SOAP Response from NDFD
I wrote a small application that pulls data from the NDFD National Digital Prediction Database and am currently getting the XML response correctly. I noticed that although I can use the WSDL2Java tool to create an interface to query weather data, when I actually use the interface to get weather data, it is returned as an XML string. Is there a cleaner way to programmatically get weather data from the resulting XML other than parsing it? For example, are there Java interfaces available so that I can call something like weatherData.getTemp () instead of going through the XML itself?
Please note that this is my first time using SOAP, so if there is anything that I am obviously ignoring please let me know.
Update:
Here is the wsdl I am using.
source to share
Assuming that:
-
you are using java
-
the XML string returned by calling the web service operation conforms to the XML schema
then you can use XMLBeans :
-
starting with XML schema, XMLBeans generates Java classes that allow you to programmatically move and process compliant XML
-
you add the generated classes to the java project
-
when you receive a response from the web service, you can create the corresponding XMLBean by paring it
-
Finally you can access XML attributes (like temp) using java getters
Assuming which weatherData
is a complex type in XMLSchema then you should be able to do something like
String xmlResponse = getWebServiceResponse();
YourXmlDocument doc = YourXmlDocument.Factory.parse(xmlResponse);
doc.getWeatherData().getTemp();
Warning: This is highly dependent on how the XML Schema is structured, so the code might be completely different. However, XMLBeans are generally very easy to use.
The downside to this solution is that if the XML schema changes, you need to rebuild your classes and recompile your code. Hopefully this shouldn't happen very often for a stable XML schema.
source to share