欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

struts2 action 解析交付的xml

更新时间: 2014-01-05 02:01:24 责任编辑: Author_N1

 

Struts2 Action 解析提交的XML
package com.bjdata.bjsearch.presentation.action.thirdinter;

import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletInputStream;

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

import com.bjdata.bjsearch.persistence.stroremanage.order.dao.impl.BiOrderProd;
import com.bjdata.bjsearch.persistence.stroremanage.order.service.IOrderService;
import com.bjdata.bjsearch.presentation.action.BaseAction;
import com.bjdata.bjsearch.util.Misc;
import com.bjdata.bjsearch.util.Util;
import com.bjdata.util.SecurityUtils;

public class DianpingInter extends BaseAction{
    private static final String LEAGUETYPE = "dianping";
    private static final Long STOREID = 4672143L;
    private static final double PERCENT=0.04;
    private String result;

    public String execute() {
        IOrderService orderService = (IOrderService) this.getServiceBean("orderService");
        //成功
        result = "succ";
        try {
            //解析xml
        	ServletInputStream in = this.getRequest().getInputStream();
            BiOrderProd prod = new BiOrderProd();
            prod.setStoreid(STOREID);
            prod.setPmid(String.valueOf(STOREID));
            prod.setProductcode("1");
            prod.setProductid("-1");
            prod.setUserid(0l);
            prod.setLeaguetype(LEAGUETYPE);
            prod.setTaskid("0");
            prod.setGetway("1");
            prod.setStatus("0");
            
            //解析 xml后 将值 给prod 了
            readXML(in,prod);
            // prod.setAmount(1);
            // prod.setOrderid(orderid);
            // prod.setOrdertime(Misc.convertString2Date(orderdate.trim(), "yyyy-MM-dd HH:mm:ss"));
            // prod.setMoney(dprice);
            // prod.setUserinfo(ouid);
            prod.setPrice(prod.getMoney()/prod.getAmount());
            prod.setCommision(prod.getMoney() * PERCENT);
            orderService.saveBiOrderProd(prod);
            String ordertime=Misc.convertTimestamp2StringTime(prod.getOrdertime(), "yyyy-MM-dd20%HH:mm:ss");
            StringBuffer urlBuffer = new StringBuffer("http://192.168.0.88:8080/myweb/orderback.jhtml?ordergetFlag=0&bid=502990&leagueflag=");
            urlBuffer.append(LEAGUETYPE).append("&cost=").append(prod.getMoney()).append("&oid=").append(prod.getOrderid()).append("&ordertime=");
            urlBuffer.append(ordertime).append("&uid=").append(prod.getUserinfo()).append("&cback=").append(prod.getCommision());
            urlBuffer.append("&mcode=").append(SecurityUtils.md5ByHex(prod.getUserinfo() + prod.getOrderid()));
            //send
            String re = Util.getHTMLByUrl(urlBuffer.toString(), 30000, "utf-8");
            if(!"0".equals(re))
                result = "0";
            
        } catch (Exception e) {
            e.printStackTrace();
            result = "3";
        }
        return HINT;
    }
    
    private void readXML(ServletInputStream in,BiOrderProd prod){
    	SAXReader saxReader=new SAXReader();
		try {
			Document document=saxReader.read(new InputStreamReader(in,"UTF-8"));
			List<Element> list=document.selectNodes("/order");
			System.out.println(list.size());
			Iterator<Element> iter=list.iterator();
			if(iter.hasNext()) {
				Element root=iter.next();
				//订单号
				Node oid=root.selectSingleNode("/order/id");
				prod.setOrderid(oid.getText());
				//下单时间
				Node otime=root.selectSingleNode("/order/order_time");
				prod.setOrdertime(Misc.convertString2Date(otime.getText(), "yyyy-MM-dd HH:mm:ss"));
				//商品数
				Node pnums=root.selectSingleNode("/order/number");
				prod.setAmount(Integer.parseInt(pnums.getText().trim()));
				//订单总金额
				Node amount=root.selectSingleNode("/order/cost/total_amount");
				prod.setMoney(Double.parseDouble(amount.getText().trim()));
				//跟单uid
				Node uid=root.selectSingleNode("/order/user/trackingcode");
				prod.setUserinfo(uid.getText());
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
    }
    

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

}

<order>
<id>1234563</id>
<user>
<channelcode>{例如baidu_wap}</channelcode>
<trackingcode>{例如uid=123456}</trackingcode>
</user>

<product>
<deal>{123142}</deal>
</product>

<order_time>yyyy-MM-dd HH:mm:ss</order_time>
<pay_time>{yyyy-MM-dd HH:mm:ss|}</pay_time>
<status>{ORDER|PAID}</status>
<number>2</number>

<cost>
<total_amount>120.2</total_amount>
</cost>

</order>

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:01:24
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/4618/
本文WWW.DOC100.NET DOC100.NET版权所有。