dom4j读取网络的xml解析并返回对象

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.wasu.kaku.entity.Coupon;


public class TestMain {

/**
* @param args
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws DocumentException, IOException {
// TODO Auto-generated method stub
String urlStr = "http://zkpartner3.mooo.com/wfjdeliver/pub/v1/coupon/filter/?cal=pat3&sk=oiu32m34&outp=full&loc=016";
URL url = new URL(urlStr);

List<Coupon> CouponArrayList=new ArrayList<Coupon>();
Document document = null;
SAXReader saxReader = new SAXReader();


// String strTest="<name>11</name>";
document = saxReader.read(url);
List Couponlist = document.selectNodes("//ApiResult/CouponList/Coupons/Coupon " );
Iterator Coupons=Couponlist.iterator();
System.out.println("店面个数为"+Couponlist.size());
while(Coupons.hasNext()){
Coupon sc = new Coupon();
Element Coupon=(Element)Coupons.next();
//name
Element name=Coupon.element("Title");
sc.setName(name.getText());
//ID
System.out.print(((Attribute )Coupon.attribute("id")).getValue());
sc.setCid(((Attribute )Coupon.attribute("id")).getValue());
System.out.println(sc.getCid());

//Advertiser
//Description
Element description=Coupon.element("Description");
sc.setDesc(description.getText());
System.out.print(description.getText());

//ImageUrl
Element imageUrl=Coupon.element("ImageUrl");
sc.setImg(imageUrl.getText());
//StartDate
Element startDate=Coupon.element("StartDate");
sc.setImg(startDate.getText());
//EndDate
Element endDate=Coupon.element("EndDate");
sc.setImg(endDate.getText());
//shop
Element advertiser=Coupon.element("Advertiser");
System.out.print(((Attribute )advertiser.attribute("name")).getValue());
sc.setShop(((Attribute )advertiser.attribute("name")).getValue());
CouponArrayList.add(sc);
}





}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值