日期:2014-05-19  浏览次数:20697 次

解析有子节点的xml字符串问题
我用sax解析xml字符串,发现当有子节点的时候,子节点的数据时获取不到的,谁能帮我改下代码?实现可以正常解析所有xml字符串。
Java code
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;
    }

发现当xml字符串如下时,cd.setCommodityName(map.get("CommodityName"));
cd.setCommodityCode(map.get("CommodityCode"));
cd.setQuantity(map.get("Quantity"));
cd.setUnitPrice(map.get("UnitPrice"));这几个是空值的,也就是没解析到。
Java code
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<