XmlAdapter与较新版本的JAXB无法正常工作 [英] XmlAdapter not working correctly with newer version of JAXB

查看:246
本文介绍了XmlAdapter与较新版本的JAXB无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下来源执行Maven项目

I'm executing a Maven Project with the below source

package com.coderplus.jaxb;

import java.util.HashMap;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(PropertiesMapAdapter.class)
public class PropertiesMap<K,V> extends HashMap<String,String>
{

}

..

package com.coderplus.jaxb;

import java.util.Map.Entry;

import javax.xml.bind.annotation.adapters.XmlAdapter;


public class PropertiesMapAdapter extends
        XmlAdapter<Properties, PropertiesMap<String, String>> {

    @Override
    public PropertiesMap<String, String> unmarshal(Properties properties)
            throws Exception {
        PropertiesMap<String, String> retVal = new PropertiesMap<String, String>();
        if (null != properties) {
            for (Property param : properties.getProperty()) {
                retVal.put(param.getName(), param.getValue());
            }
        }
        return retVal;
    }

    @Override
    public Properties marshal(PropertiesMap<String, String> propertiesMap)
            throws Exception {
        Properties properties = new Properties();
        if (propertiesMap != null) {
            for (Entry<String, String> entry : propertiesMap.entrySet()) {
                Property param = new Property();
                param.setName(entry.getKey());
                param.setValue(entry.getValue());
                properties.getProperty().add(param);
            }
        }
        return properties;
    }
}

..

package com.coderplus.jaxb;  
import javax.xml.bind.*;


public class Demo {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        Root root = new Root();
        PropertiesMap map = new PropertiesMap();
        map.put("hello", "World");
        map.put("name", "value");
        root.setProperties(map);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

...

src / main / resources中的模式

Schema in src/main/resources

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="Properties">
        <xsd:sequence>
            <xsd:element name="Property" type="Property" minOccurs="0"
                maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Property">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:attribute name="name" type="xsd:string" />
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>

    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Properties" type="Properties" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema> 

src / main / resources中的绑定文件

Binding file in src/main/resources

<jaxb:bindings version="2.0"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jaxb:bindings schemaLocation="map.xsd">
        <jaxb:bindings node="//xs:element[@name='Properties']">
            <jaxb:property>
                <jaxb:baseType name="com.coderplus.jaxb.PropertiesMap&lt;String,String&gt;" />
            </jaxb:property>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

最后是pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.coderplus.jaxb</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.9.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <generatePackage>com.coderplus.jaxb</generatePackage>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

当我使用JDK 1.6执行它时,Demo Class产生以下输出

The Demo Class produces the below output when I execute it using JDK 1.6

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Properties>
        <Property name="hello">World</Property>
        <Property name="name">value</Property>
    </Properties>
</Root>

但出于某种原因,它会生成以下JDK 1.7及更高版本(更新的JAXB?)

but for some reason, it generates the below with JDK 1.7 and up (newer JAXB?)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <properties>
        <entry>
            <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">hello</key>
            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">World</value>
        </entry>
        <entry>
            <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">name</key>
            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">value</value>
        </entry>
    </properties>
</Root>

如何使用JDK 1.7或更新版本的JAXB?

How can I get it working on JDK 1.7 or the newer version of JAXB?

更多信息:

maven-jaxb2-plugin与其他人一起生成以下课程

The maven-jaxb2-plugin generates the below class along with others

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "properties"
})
@XmlRootElement(name = "Root")
public class Root {

    @XmlElement(name = "Properties", required = true, type = Properties.class)
    protected PropertiesMap<String, String> properties;

    public PropertiesMap<String, String> getProperties() {
        return properties;
    }

    public void setProperties(PropertiesMap<String, String> value) {
        this.properties = value;
    }

}

如果我手动进入并添加注释 @XmlJavaTypeAdapter(PropertiesMapAdapter.class)如下面的代码所示,然后它可以工作

If I manually go in and add the annotation @XmlJavaTypeAdapter(PropertiesMapAdapter.class) like in the code below, then it works

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "properties"
})
@XmlRootElement(name = "Root")
public class Root {

    @XmlElement(name = "Properties", required = true, type = Properties.class)
    @XmlJavaTypeAdapter(PropertiesMapAdapter.class)
    protected PropertiesMap<String, String> properties;

    public PropertiesMap<String, String> getProperties() {
        return properties;
    }

    public void setProperties(PropertiesMap<String, String> value) {
        this.properties = value;
    }

}

我如何制作maven- jaxb2-plugin自动添加 XmlJavaTypeAdapter ?如果有帮助,此链接包含压缩的Maven项目

How can I make the maven-jaxb2-plugin to add the XmlJavaTypeAdapter automatically? In case it would help, this link has the zipped Maven Project

推荐答案

尝试添加 xjc :javaType 自定义。

<xjc:javaType name="org.acme.foo.PropertiesMap"
 adapter="org.acme.foo.PropertiesMapAdapter"/>

这篇关于XmlAdapter与较新版本的JAXB无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆