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()));//跟单uidNode 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>