小编cod*_*eak的帖子

解析Web服务响应时出现MalformedURLException

所以我正在尝试编写一个简单的Web服务来获取当前的天气.使用以下代码,可以调用Web服务方法,并可以将返回的XML数据打印到控制台.

但是,当我尝试解析此方法响应时,我得到MalformedURLException.我可以看到我想在错误中解析的XML.

所以我尝试将响应保存到文件中以解析它:

当我尝试将Web响应保存到文件时,我会收到所有中文字母.System.out.println将XML完美地打印到控制台,就像我需要它在文件中一样.

我是一个新手,所以请原谅我,如果我错过了一些非常简单的事情.我想这样做而不必在本地保存文件,但无论什么工作都很好.

我的问题出在这段代码中:

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 …
Run Code Online (Sandbox Code Playgroud)

java xml parsing

3
推荐指数
1
解决办法
538
查看次数

标签 统计

java ×1

parsing ×1

xml ×1