Gson-Xml Android xml转object对象

前端之家收集整理的这篇文章主要介绍了Gson-Xml Android xml转object对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

链接

原始版本
改动后的版本
改动:
1.在java项目,xml转义问题
2.对map的支持


项目Gradle引用
gradle
compile('com.stanfy:gson-xml-java:0.1.+') {  
    exclude group: 'xmlpull',module: 'xmlpull'
}
//如果想更新gson的版本
compile('com.stanfy:gson-xml-java:0.1.+') { 
    exclude group: 'xmlpull',module: 'xmlpull' 
    exclude group: 'com.google.code.gson',module: 'gson'
}
compile 'com.google.code.gson:gson:2.6.1'

eclipse则需要把gson的jar以及gsom-xml的jar引用即可。

示例github代码

package com.stanfy.gsonxml.test;

import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.stanfy.gsonxml.GsonXmlBuilder;

import org.junit.Test;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class DemoTest extends AbstractXmlTest {

    public static class A {
        public A() {
            maps = new LinkedHashMap<String,B>();
        }
        //@代表这个字段是属性属性一般不支持复杂类型
        @SerializedName("@id")
        long id;
        //不带@则是子标签
        @SerializedName("name")
        List<String> names;
        @SerializedName("b")
        List<B> bs;

        @SerializedName("id")
        int[] is;
        @SerializedName(value = "map")
        Map<String,B> maps;
        //List<MyMap> maps;

        @SerializedName("type")
        C c;
    }

    public static class MyMap {
        @SerializedName("key")
        String key;
        @SerializedName("value")
        B value;
    }

    public static class B {
        @SerializedName("@id")
        long id;
        //$则是xml的标签文字
        @SerializedName("$")
        String text;

        @SerializedName("@type")
        C c;
    }

    public enum C {
        a,b,c
    }

    final static String TEST_XML =
            "       <a id=\"123\">" +
                    "   <name>test name1</name>" +
                    "   <name>test name2</name>" +
                    "   <b id=\"1234567890\">b name1</b>" +
                    "   <b id=\"0123\">b name2</b>" +
                    "   <map>" +
                    "       <key>hello</key>" +
                    "       <value id=\"1\">world</value>" +
                    "   </map>" +
                    "   <map>" +
                    "       <value id=\"1\" type=\"a\" >name</value>" +
                    "       <key>hello2</key>" +
                    "   </map>" +
                    "   <id>1</id>" +
                    "   <id>2</id>" +
                    "   <id>3</id>" +
                    "   <type>b</type>" +
                    "</a>";
    final static String TEST_XML_MAP =
            "       <a>" +
                    "   <map>" +
                    "       <key>hello</key>" +
                    "       <value id=\"1\">world</value>" +
                    "   </map>" +
                    "   <map>" +
                    "       <value id=\"1\" type=\"a\" >name</value>" +
                    "       <key>hello2</key>" +
                    "   </map>" +
                    "</a>";

    @Test
    public void test() {
        //复杂对象的转换
        GsonBuilder builder = new GsonBuilder();
        builder.enableComplexMapKeySerialization();
        //自定义Enum显示为字符串
        builder.registerTypeAdapter(C.class,new TypeAdapter<C>() {
            @Override
            public C read(JsonReader in) throws IOException {
                String value = in.nextString();
                C[] cs = C.values();
                for (C c : cs) {
                    if (c.toString().equalsIgnoreCase(value)) {
                        return c;
                    }
                }
                return null;
            }

            @Override
            public void write(JsonWriter out,C value) throws IOException {
                out.value(value == null ? "" : value.toString());
            }
        });
        final A a = new GsonXmlBuilder()
                .setXmlParserCreator(SimpleXmlReaderTest.PARSER_CREATOR)
                .wrap(builder)
                .setPrimitiveArrays(true)
                .setSameNameLists(true)
                .create()
                .fromXml(TEST_XML,A.class);
        assertEquals("test name2",a.names.get(1));
        assertEquals(1234567890,a.bs.get(0).id);
        assertEquals("b name2",a.bs.get(1).text);
        assertEquals(2,a.bs.size());
        assertEquals(C.b,a.c);
        assertEquals("world",a.maps.get("hello").text);
        assertEquals("name",a.maps.get("hello2").text);
        assertEquals(C.a,a.maps.get("hello2").c);
        assertEquals(2,a.maps.size());
        assertEquals(3,a.is.length);
        assertEquals(LinkedHashMap.class,a.maps.getClass());
//        assertEquals(2,a.maps.get(1).value.id);
    }

    @Test
    public void testRootMap() {
        //单个map对象的转换
        GsonBuilder builder = new GsonBuilder();
        builder.enableComplexMapKeySerialization();
        //自定义Enum显示为字符串
        builder.registerTypeAdapter(C.class,C value) throws IOException {
                out.value(value == null ? "" : value.toString());
            }
        });
        Map<String,B> maps = new GsonXmlBuilder()
                .setXmlParserCreator(SimpleXmlReaderTest.PARSER_CREATOR)
                .wrap(builder)
                //跳过跟标签,单map,list必须设置
                .setSkipRoot(true)
                 //map必须设置
                .setPrimitiveArrays(true)
                //map必须设置
                .setSameNameLists(true)
                .create()
                .fromXml(TEST_XML_MAP,new TypeToken<Map<String,B>>() {
                }.getType());
        assertEquals("world",maps.get("hello").text);
        assertEquals("name",maps.get("hello2").text);
        assertEquals(C.a,maps.get("hello2").c);
        assertEquals(2,maps.size());
    }
}
原文链接:https://www.f2er.com/xml/295490.html

猜你在找的XML相关文章