1. 程式人生 > >遍歷xml並把結果放到map中

遍歷xml並把結果放到map中

/**
* 遍歷解析xml
* @param infoXML
* @return
*/
public static Map<String, Object> doXml(String infoXML) {  
        Document document;  
        Map<String, Object> map = new HashMap<String, Object>();  
        try {  
            document = DocumentHelper.parseText(infoXML);  
            Element root = document.getRootElement();  
            Iterator it = root.elements().iterator();  
            while (it.hasNext()) {  
                Element info = (Element) it.next();  
                map.put(info.getName(), info.getText());  
                Iterator itc = info.elements().iterator();  
                while (itc.hasNext()) {  
                    Element infoc = (Element) itc.next();  
                    map.put(infoc.getName(), infoc.getText());  
                }  
            }  
        } catch (DocumentException e) {  
            e1.printStackTrace();  
        }  
        return map;  
    }