JAXB:使用 XmlJavaTypeAdapter 时如何抑制周围的 XmlElement?

2023-12-23

I use @XmlJavaTypeAdapter转变Map<String, MapItem>物体进入List<MapItem>编组时(反编组时反之亦然。)

该列表总是有一个周围的@XmlElement我想摆脱它,因为它使生成的 XML 变得混乱。

如何才能做到这一点?

或者,换句话说,我怎样才能摆脱元素map在以下 XML 中:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<top>
    <map>
        <item val="some-val" key="some-key"/>
    </map>
</top>

Class MapItemis a simple class with a key and a value:
public static class MapItem {
    @XmlAttribute(name = "key")
    String key;

    @XmlAttribute(name = "val")
    String val;
}

The declaration of Map<String, MapItem> either implicitly or explicitly contains an @XmlElement annotation:
@XmlJavaTypeAdapter(MyMapItemAdapter.class)
@XmlElement(name = "map")
Map<String, MapItem> map = new TreeMap<String, MapItem>();

The classes for @XmlJavaTypeAdapter:
@XmlType(name = "map-type", propOrder = { "list" })
static class MyMapItemType {
    @XmlElement(name = "item")
    List<MapItem> list = new ArrayList<MapItem>();
}

@XmlTransient
static final class MyMapItemAdapter extends
        XmlAdapter<MyMapItemType, Map<String, MapItem>> {

    MyMapItemAdapter() {
    }

    @Override
    public MyMapItemType marshal(Map<String, MapItem> arg0)
            throws Exception {
        MyMapItemType myMapType = new MyMapItemType();
        for (Entry<String, MapItem> entry : arg0.entrySet()) {
            myMapType.list.add(entry.getValue());
        }
        return myMapType;
    }

    @Override
    public Map<String, MapItem> unmarshal(MyMapItemType arg0)
            throws Exception {
        TreeMap<String, MapItem> treeMap = new TreeMap<String, MapItem>();
        for (MapItem myEntryType : arg0.list) {
            treeMap.put(myEntryType.key, myEntryType);
        }
        return treeMap;
    }
}

The top class declaration:
@XmlRootElement(name = "top")
@XmlType(name = "top")
@XmlAccessorType(XmlAccessType.FIELD)
public class JaxbMapTest {

My complete test class:
package xml;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "top")
@XmlType(name = "top")
@XmlAccessorType(XmlAccessType.FIELD)
public class JaxbMapTest {

    public static class MapItem {
        @XmlAttribute(name = "key")
        String key;

        @XmlAttribute(name = "val")
        String val;
    }

    @XmlTransient
    static final class MyMapItemAdapter extends
            XmlAdapter<MyMapItemType, Map<String, MapItem>> {

        MyMapItemAdapter() {
        }

        @Override
        public MyMapItemType marshal(Map<String, MapItem> arg0)
                throws Exception {
            MyMapItemType myMapType = new MyMapItemType();
            for (Entry<String, MapItem> entry : arg0.entrySet()) {
                myMapType.list.add(entry.getValue());
            }
            return myMapType;
        }

        @Override
        public Map<String, MapItem> unmarshal(MyMapItemType arg0)
                throws Exception {
            TreeMap<String, MapItem> treeMap = new TreeMap<String, MapItem>();
            for (MapItem myEntryType : arg0.list) {
                treeMap.put(myEntryType.key, myEntryType);
            }
            return treeMap;
        }
    }

    @XmlType(name = "map-type", propOrder = { "list" })
    static class MyMapItemType {
        @XmlElement(name = "item")
        List<MapItem> list = new ArrayList<MapItem>();
    }

    public static void main(String[] args) {

        try {

            // Setup object
            JaxbMapTest jaxbMapTest = new JaxbMapTest();
            MapItem mapItem = new MapItem();
            mapItem.key = "some-key";
            mapItem.val = "some-val";
            jaxbMapTest.add(mapItem);

            // Marshal
            JAXBContext jaxbContext = JAXBContext
                    .newInstance(JaxbMapTest.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            marshaller.marshal(jaxbMapTest, new File("JaxbMapTest.out"));

            // Exit
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    @XmlJavaTypeAdapter(MyMapItemAdapter.class)
    @XmlElement(name = "map")
    Map<String, MapItem> map = new TreeMap<String, MapItem>();

    void add(MapItem mapItem) {
        map.put(mapItem.key, mapItem);
    }
}

Note:我是EclipseLink JAXB (MOXy) http://www.eclipse.org/eclipselink/moxy.php领导者和成员JAXB (JSR-222) http://jcp.org/en/jsr/detail?id=222专家组。

你可以使用@XmlPath(".")MOXy 中的扩展来映射此用例。指定"."因为路径指示子对象的内容应写入父对象元素中。

import java.util.HashMap;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlJavaTypeAdapter(MyMapItemAdapter.class)
    @XmlPath(".")
    Map<String, MapItem> map = new TreeMap<String, MapItem>();       

}

完整示例

  • JAXB 封送由 XmlAdapter 创建的 ArrayList https://stackoverflow.com/questions/13163430/jaxb-marshal-an-arraylist-created-by-xmladapter/13163680#13163680

了解更多信息

  • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JAXB:使用 XmlJavaTypeAdapter 时如何抑制周围的 XmlElement? 的相关文章

随机推荐