So I am trying to write a simple web service that gets the current weather. With the following code the web service method can be called and the returned XML data can be printed to the console.
However, when I try to parse this method response I get MalformedURLException. I can see the XML I want to parse in the error.
So I tried saving the response to a file to parse it that way:
When I try saving the web response to a file I get all Chinese letters. System.out.println prints out the XML perfectly to the console like I need it to be in the file.
I am a total newb so please excuse me if I am missing something very simple. I would like to do this without having to save the file locally, but whatever works is good here.
My problem lies somewhere in this code:
GlobalWeather service = new GlobalWeather();
GlobalWeatherSoap port = service.getGlobalWeatherSoap();
String data = port.getWeather(city, country);
// this prints the xml response to the console perfectly
System.out.println(data);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new WeatherApp());
// this gives MalformedURLexception when set up this way.
// this gives the correct output when passed a locally stored XML file
// I have omitted my SAX methods and my weather class that holds the data
// but all work fine when using a local XML file on my machine.
xmlReader.parse(data);
Replace
with
You are accidentally calling the wrong overloading of SAXReader.parse. The version that takes a String expects a URI where it can retrieve the content to parse, not the content itself.