Java: Accessing XML value as a new variable in another class

546 Views Asked by At

Java Programming

I have a class WeatherAgent. The function of this class is to get the current temperature value and other parameters from a URL (XML code) from World weather online.

My main question is; how to get the generated temperature value in class WeatherAgent into antother class (Boiler)? So, Accessing XML value in class WeatherAgent as a new variable in another class (class Boiler).

package assignment_4;


    /*
     * imports of class WeatherAgent2
     */
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;


    /*
     *start class WeatherAgent2  
     */

    public class WeatherAgent {

        private Document getDocument() {

            Document doc = null;

            try {

                URL url = new URL(
                        "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Eindhoven&format=xml&num_of_days=5&key=87xdd77n893f6akfs6x3jk9s");

                URLConnection connection = url.openConnection(); // Connecting to
                                                                    // URL specified

                doc = parseXML(connection.getInputStream());

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            return doc;

        }

        private Document parseXML(InputStream stream) throws Exception

        {

            DocumentBuilderFactory objDocumentBuilderFactory = null;

            DocumentBuilder objDocumentBuilder = null;

            Document doc = null;

            try

            {

                objDocumentBuilderFactory = DocumentBuilderFactory.newInstance();

                objDocumentBuilder = objDocumentBuilderFactory.newDocumentBuilder();

                doc = objDocumentBuilder.parse(stream);
            }

            catch (Exception ex)

            {

                throw ex;

            }

            return doc;

        }



        /*
         * get the temperature
         */
        public double temperature() {

            Document doc = getDocument();

            Node Temperature;

            Double temp = 0.0;

            NodeList forecast = doc.getElementsByTagName("temp_C");

            for (int i = 0; i < forecast.getLength(); i++) {

                Element element = (Element) forecast.item(i);

                NodeList list1 = (NodeList) element.getChildNodes();

                Temperature = list1.item(0);

                temp = Double.parseDouble(Temperature.getNodeValue());

            }

            return temp;

        }

        /*
         * get cloud cover
         */
        public double cloudcover() {

            Document doc = getDocument();

            Node Cloudcover;

            Double cloudcover = 0.0;

            NodeList forecast = doc.getElementsByTagName("cloudcover");

            for (int i = 0; i < forecast.getLength(); i++) {

                Element element = (Element) forecast.item(i);

                NodeList list2 = (NodeList) element.getChildNodes();

                Cloudcover = list2.item(0);

                cloudcover = Double.parseDouble(Cloudcover.getNodeValue());

            }

            return cloudcover;

        }





        /*
         * print method
         */
        public static void main(String[] argvs) {

            WeatherAgent wp = new WeatherAgent();

            System.out.println("Temperature: " + wp.temperature());

            System.out.println("Cloudcover: " + wp.cloudcover());

        }

    }

I have this methode in class Boiler to get the temperature value from class WeatherAgent into class Boiler. It is not working and I do know why. Thanks already for your help!

public double getValueFromAgent(){


         double agentTemp = send(URL.create("http://api.worldweatheronline.com/free/v1/weather.ashx?q=Eindhoven&format=xml&num_of_days=5&key=87xdd77n893f6akfs6x3jk9s", "temperature" , null, Double.class));

                 return agentTemp;

     }
1

There are 1 best solutions below

0
On

There are design patterns for how to structure your classes to share information like this. I'd look into the Observer pattern and the Mediator pattern.

Or in main, when you get the temperature from the WeatherAgent, push it into Boiler. With all of these, the idea is to decouple Boiler from knowing anything about WeatherAgent. It helps with reusability, and makes testing them independently a lot easier.