日期:2014-05-19 浏览次数:20697 次
public static MessageData analyzeXML(String value){ MessageData md = new MessageData(); Commoditys cd = new Commoditys(); StringReader read = new StringReader(value); InputSource source = new InputSource(read); SAXBuilder saxb = new SAXBuilder(); // List result = null; Map<String,String> map = new HashMap<String,String>(); try { // result = new ArrayList(); Document doc = saxb.build(source); Element root = doc.getRootElement(); List node = root.getChildren(); Element et = null; for (int i = 0; i < node.size(); i++) { et = (Element) node.get(i); List subNode = et.getChildren(); Element subEt = null; for (int j = 0; j < subNode.size(); j++) { subEt = (Element) subNode.get(j); map.put(subEt.getName(), subEt.getText()); } // if (map.size() > 0) // result.add(map); } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } md.setBarCode(map.get("BarCode")); md.setOrderNumber(map.get("OrderNumber")); md.setRecipientName(map.get("RecipientName")); md.setRecipientAddress(map.get("RecipientAddress")); md.setRecipientCode(map.get("RecipientCode")); md.setRecipientPhone(map.get("RecipientPhone")); md.setPayment(map.get("Payment")); md.setWeight(map.get("Weight")); md.setPostage(map.get("Postage")); md.setServiceCharge(map.get("ServiceCharge")); md.setPostagePostPaidFlag(map.get("PostagePostPaidFlag")); md.setMailType(map.get("MailType")); md.setOrderFlag(map.get("OrderFlag")); md.setDescription(map.get("Description")); md.setSign(map.get("Sign")); md.setMerchantPublicCert(map.get("MerchantPublicCert")); cd.setCommodityName(map.get("CommodityName")); cd.setCommodityCode(map.get("CommodityCode")); cd.setQuantity(map.get("Quantity")); cd.setUnitPrice(map.get("UnitPrice")); md.setCommoditys(cd); return md; }
StringBuffer xml = new StringBuffer(); xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); xml.append("<Result xmlns=\"http://www.fiorano.com/fesb/activity/DBQueryOnInput2/Out\">"); xml.append("<Content>"); xml.append("<BarCode>424244244 </BarCode >"); xml.append("<OrderNumber>42244</OrderNumber >"); xml.append("<RecipientName>ffff<