访问url返回xml 转换成list

1:maven 包

<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
</dependency>

 2:代码:

public static QueryServiceBandwidthData getContrastBandwidth(String url) {
		Digester digester = new Digester();
		Rules rules = digester.getRules();
		rules.add("BandWidthDatas", new ObjectCreateRule(QueryServiceBandwidthData.class));
		rules.add("BandWidthDatas/Result", new BeanPropertySetterRule("result"));
		rules.add("BandWidthDatas/InData", new BeanPropertySetterRule("inData"));
		rules.add("BandWidthDatas/OutData", new BeanPropertySetterRule("outData"));
		rules.add("BandWidthDatas/TheData", new BeanPropertySetterRule("theData"));
		rules.add("BandWidthDatas/InFlux", new BeanPropertySetterRule("inFlux"));
		rules.add("BandWidthDatas/OutFlux", new BeanPropertySetterRule("outFlux"));
		rules.add("BandWidthDatas/TotalFlux", new BeanPropertySetterRule("totalFlux"));

		InputStream in = null;
		QueryServiceBandwidthData bandWidthDatas = null;
		try {
			in = new URL(url).openStream();
			bandWidthDatas = (QueryServiceBandwidthData) digester.parse(in);
		} catch (MalformedURLException e) {
			logger.error("read queryService xml error for MalformedURLException", e);
		} catch (IOException e) {
			logger.error("read queryService xml error for IOException", e);
		} catch (SAXException e) {
			logger.error("read queryService xml error for SAXException", e);
		}
		bandWidthDatas.setStartTime(url.substring(url.indexOf("&StartTime="), url.indexOf("&EndTime=")));
		bandWidthDatas.setEndTime(url.substring(url.indexOf("&EndTime="), url.indexOf("&ChannelID1")));
		return bandWidthDatas;
	}

 3:url返回的结果

<?xml version="1.0" encoding="GB2312"?>
<BandWidthDatas>
   <Result>true</Result>
<InData>0,0</InData>
<OutData>0,0</OutData>
<TheData>0,0</TheData>
   <InFlux>0</InFlux>
   <OutFlux>0</OutFlux>
   <TotalFlux>0</TotalFlux>
</BandWidthDatas>

 

4:bean的属性

private String result;
	private String inData;
	private String outData;
	private String theData;
	private String inFlux;
	private String outFlux;
	private String totalFlux;
	private Date startTime;
	private Date endTime;
	private String productCode;

 总结:

 

rules.add("BandWidthDatas", new ObjectCreateRule(QueryServiceBandwidthData.class)); 
                        xml根标签                                               bean类

 

rules.add("BandWidthDatas/Result", new BeanPropertySetterRule("result")); 
                                 根标签/子标签                                                 bean的属性

 

你可能感兴趣的:(list)