采用DOM4J中的SAXReader解析webservice返回的XML文件

1.  通过URL连接GET返回的webservice内容,如下代码:

     

			String str = FORECAST_WEATHER_URL + this.getPYName(exter.getCity_code());
			log.info("天气预报查询str is " + str);
			URL url = new URL(str);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setConnectTimeout(15000);
			conn.setReadTimeout(15000);
			conn.setRequestMethod("GET");
			InputStream in = conn.getInputStream();
			
//			FileInputStream in = new FileInputStream(new File("d:/weather.xml"));
			
			byte[] bytes = IoUtil.InputStreamToByte(in); 
			InputStream inputStream = new ByteArrayInputStream(bytes); 
			InputStreamReader strInStream = new InputStreamReader(inputStream, "GBK");
			
			SAXReader reader = new SAXReader();
//			reader.setEncoding("UTF-8");
			Document doc = reader.read(strInStream);
			//进行当天天气预报的查询
			List currentList = XMLParseUtil.getParseMapFromXMLStream(doc,XMLPathConstants.CURRENT_WEATHER_XPATH);
			//进行未来天气预报的查询
			List forecastList = XMLParseUtil.getParseMapFromXMLStream(doc,XMLPathConstants.FORECAST_WEATHER_XPATH);

	public final static String CURRENT_WEATHER_XPATH = "//xml_api_reply/weather/current_conditions";
	public final static String FORECAST_WEATHER_XPATH = "//xml_api_reply/weather/forecast_conditions";

2.  通过如下的方法解析XML中的内容

	public static List getParseMapFromXMLStream(Document doc,String parentPath) throws DocumentException {

		List rowList = doc.selectNodes(parentPath);
		Map map = null;
		List list = null;
		if(rowList != null && rowList.size() >0) {
			map = new HashMap();
			list = new ArrayList();
			for(Iterator iter = rowList.iterator();iter.hasNext();){  
	            //获得具体的节点的父元素   
	            Element element = (Element)iter.next();  
	            //获得具体的节点的父元素的属性  
//	            List elementList = element.attributes();  
//	            for(Iterator iter1 = elementList.iterator();iter1.hasNext();){  
//	                //将每个属性转化为一个抽象属性,然后获取其名字和值  
//	                AbstractAttribute aa = (AbstractAttribute)iter1.next();  
//	                System.out.println("Name:"+aa.getName()+";Value:"+aa.getValue());  
//	            }  
//				如果element下有子元素,(类似width="**"),要想获得该子元素的值,可以用如下方法  
//				System.out.println(element.elementText("test"));
	            
				//获得父节点内的各种借点或者属性
				Iterator it1 = element.elementIterator();
				while(it1.hasNext()) {
					Element element1 = (Element)it1.next();  
		            //获得子节点的所有列表  
		            List elementList1 = element1.attributes();  
		            for(Iterator it2 = elementList1.iterator();it2.hasNext();){  
		                //将每个属性转化为一个抽象属性,然后获取其名字和值  
		                AbstractAttribute aa = (AbstractAttribute)it2.next();  
//		                System.out.println("Name11:"+aa.getName()+";Value11:"+aa.getValue());  
		                //这边需要添加借点的名字为KEY的值(重要)
		                map.put(element1.getName(), aa.getValue());
		            }
				}
				list.add(map);
			}
		}	
		return list;
	}

3.   获取的XML内容为:



	
		
			
			
			
			
			
			
			
		

		
			
			
			
			
			
		
		
		
		    测试用的
			
			
			
			
		
		
		
			
			
			
			
		
		
		
			
			
			
			
		
		
		
			
			
			
			
		
	





你可能感兴趣的:(JAVA技术)