亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2348 | 回復: 0
打印 上一主題 下一主題

通用的解析xml文件并封裝成FormBean的方法 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-09-12 11:59 |只看該作者 |倒序瀏覽

public class PraseXmlUtil {
    @SuppressWarnings("unchecked")
    public static Object praseXml(String xPath,String xml,Object form)
            throws Exception, DocumentException {
        List selectNodes = getRootElement(read(xml)).selectNodes(xPath);
        return getFormBean(form, treeWalk(selectNodes));
        
    }
    /**
     * ################################################################################### #
     * 返回該文檔的根元素 # #
     *
     * @param doc #
     * @return #
     * ###################################################################################
     */
    public static final Element getRootElement(Document doc) {
        return doc.getRootElement();
    }
    /**
     * ################################################################################### #
     * 返回單結點下的值
     *
     * @param String #
     * @return #
     * ###################################################################################
     * @throws DocumentException
     * @throws MalformedURLException
     */
    public static final String getSingleElementValue(String xPath,String xml) throws DocumentException {
        return getRootElement(read(xml)).selectSingleNode(xPath).getText();
    }
    /**
     * ################################################################################### #
     * Xml 轉 String # #
     *
     * @param document #
     * @return #
     * ###################################################################################
     */
    @SuppressWarnings("unused")
    private String getString(Document document) {
        return document.asXML();
    }
    /**
     * ################################################################################### #
     * 讀入一個xml格式字符串,返回這個Document # #
     *
     * @param fileName #
     * @return #
     * @throws MalformedURLException #
     * @throws DocumentException #
     * ###################################################################################
     */
    public static final Document read(String xml)throws DocumentException {
        Document document = DocumentHelper.parseText(xml);
        return document;
    }
    /**
     * ################################################################################### #
     * 遍歷傳入元素下面所有節(jié)點 # #
     *
     * @param element #
     * ###################################################################################
     */
    @SuppressWarnings("unchecked")
    private static HashMapString, ArrayList> treeWalk(Element element,
            HashMapString, ArrayList> map) {
        for (int i = 0, size = element.nodeCount(); i  size; i++) {
            Node node = element.node(i);
            if (node instanceof Element) {
               
                    if (!map.containsKey(node.getName())) {
                        ArrayListString> temp = new ArrayListString>();
                        temp.add(node.getText().trim());
                        map.put(node.getName(), temp);
                    } else {
                        ArrayListString> temp = map.get(node.getName());
                        temp.add(node.getText().trim());
                        map.put(node.getName(), temp);
                    }
                treeWalk((Element) node, map);
            }
        }
        return map;
    }
    /**
     *
     * ################################################################################### #
     * 遍歷所有節(jié)點 # #
     *
     * @param fileName #
     * @throws Exception #
     * ###################################################################################
     */
    public static HashMap treeWalk(ListNode> nodes) {
        HashMapString, ArrayList> map = new HashMapString, ArrayList>();
        for (int i = 0; i  nodes.size(); i++) {
            Element element = (Element) nodes.get(i);
            treeWalk(element, map);
        }
        return map;
    }
    /**
     * 利用反射得到該xml所對應的formbean的對象
     *
     * @param obj
     * @param fieldName
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     */
    @SuppressWarnings("unchecked")
    private static Object getFormBean(Object form,
            HashMapString, ArrayList> map) throws SecurityException,
            IllegalArgumentException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException,
            InstantiationException {
        Class?> classType = form.getClass();
        Field[] fields = classType.getDeclaredFields();// 得到此類所有的字段
        Method method = null;
        for (int i = 0; i  fields.length; i++) {
            String tempField = fields.getName();
            String tempMethod = "set" + tempField.substring(0, 1).toUpperCase()
                    + tempField.substring(1);// 根據(jù)字段名拼湊其set方法名
            ArrayListString> temp = map.get(tempField);
            if (temp == null) {
                continue;
            }
String xmlValue = temp.toString().substring(1,temp.toString().length() - 1);
            Class cls[] = new Class[1];
            Object obj[] = new Object[1];
            // 得到每個字段的數(shù)據(jù)類型,也即是set方法的參數(shù)類型
     if (fields.getType().isArray()) {    //如果這個屬性是String[]類型
                Class?> clazz = fields.getType();
                cls = new Class[] { clazz };
                obj = new Object[] { clazz.cast(xmlValue.split(",")) };
            } else {                   //如果這個屬性是其他基本數(shù)據(jù)類型
    Class?> clazz = fields.getType();
    Constructor?> constructor = clazz.getConstructor(new Class[] { String.class });
                cls = new Class[] { clazz };
                obj = new Object[] { constructor.newInstance(xmlValue) };
            }
            method = classType.getMethod(tempMethod, cls);
            method.invoke(form, obj);
        }
        return form;
    }
}

下面是運行的demo

文件:
AutoPraseXml.rar
大小:
499KB
下載:
下載


本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u1/57965/showart_1188751.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP