拜访url回来xml 转换成listITeye - AG环亚娱乐

拜访url回来xml 转换成listITeye

2019年02月28日12时39分47秒 | 作者: 问凝 | 标签: 标签,特点,代码 | 浏览: 3051

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的特点

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章