实现将xml与Map互相转换的方法

1 使用vtd技术将xml转换为Map

    1)引入maven依赖:

        
            com.ximpleware
            vtd-xml
            2.13

        

   2)转换过程:

package com.wj.business.util;
import com.ximpleware.*;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
public class ClientResponseUtil2 {
    private static final String UTF8 = "UTF-8";
    public static void main(String args[]) throws UnsupportedEncodingException, ParseException, XPathParseException, NavException, XPathEvalException {
        String resourceXml = "\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" +
                "\n" +
                "    \"January\">January\n" +
                "    \"February\">February\n" +
                "    \"March\">March\n" +
                "    \"April\">April\n" +
                "    \"May\">May\n" +
                "    \"June\">June\n" +
                "    \"July\">July\n" +
                "    \"August\">August\n" +
                "    \"September\">September\n" +
                "    \"October\">October\n" +
                "    \"November\">November\n" +
                "    \"December\">December\n" +
                "    \"Today\">Today\n" +
                "    \"Tomorrow\">Tomorrow\n" +
                "\n" +
                "";
        VTDGen vg = new VTDGen();
        vg.setDoc(resourceXml.getBytes(UTF8));
        vg.parse(false);
        VTDNav vn = vg.getNav();
        //利用AutoPilot进行节点遍历
        AutoPilot ap = new AutoPilot();
        int i = -1;
        ap.bind(vn);
        ap.selectXPath("/resources");
        HashMap, Object> map = new HashMap, Object>();
        while ((i = ap.evalXPath()) != -1) {
            if (vn.toElement(VTDNav.FIRST_CHILD)) {
                do {
                    if (vn.getText()!=-1){
                        map.put(vn.toString(vn.getAttrVal("name")), vn.toString(vn.getText()));
                    }
                } while (vn.toElement(VTDNav.NEXT_SIBLING));
            }
            vn.toElement(VTDNav.PARENT);
        }
        System.out.println(map);

    }



}

使用的xml为:

    January
    February
    March
    April
    May
    June
    July
    August
    September
    October
    November
    December
    Today
    Tomorrow

说明:这个例子取的是元素的具体的值,也可以通过方法:

vn.toString(vn.getCurrentIndex())(获取具体标签的值)替换代码中的 vn.toString(vn.getAttrVal("name"))(获取name元素内容)来获取到元素的Key值,来解析格式为"wj18"的xml.

2 将Map转换为xml格式(与上述操作可逆)

import java.util.*;

public class Test {
    public static String callMapToXML(Map map) {
        StringBuffer sb = new StringBuffer();
        sb.append("\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>");
        mapToXMLTest2(map, sb);
        sb.append("");
        return sb.toString();
    }

    private static void mapToXMLTest2(Map map, StringBuffer sb) {
        Set set = map.keySet();
        for (Iterator it = set.iterator(); it.hasNext();) {
            String key = (String) it.next();
            Object value = map.get(key);
            if (null == value)
                value = "";
            if (value.getClass().getName().equals("java.util.ArrayList")) {
                ArrayList list = (ArrayList) map.get(key);
                sb.append("<" + key + ">");
                for (int i = 0; i < list.size(); i++) {
                    HashMap hm = (HashMap) list.get(i);
                    mapToXMLTest2(hm, sb);
                }
                sb.append("+ key + ">");

            } else {
                if (value instanceof HashMap) {
                    sb.append("<" + key + ">");
                    mapToXMLTest2((HashMap) value, sb);
                    sb.append("+ key + ">");
                } else {
                    /*返回这种格式的xml会直接执行如下代码*/
                    sb.append("\"" + key + "\">" + value + "");
                }

            }

        }
    }
    public static void main(String args[]){
        Map,Object> map =new HashMap, Object>();
        map.put("app_name","testApp");
        map.put("search_label","Dictionary");
        map.put("search_hint","Search the dictionary");
        String ss = Test.callMapToXML(map);
        System.out.println(ss);


    }

}

你可能感兴趣的:(idea,maven,vtd,xml格式转换)