访问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的属性

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值