gpt4 book ai didi

java - XmlAdapter 无法与较新版本的 JAXB 一起正常工作

转载 作者:搜寻专家 更新时间:2023-11-01 03:51:39 26 4
gpt4 key购买 nike

我正在使用以下源代码执行一个 Maven 项目

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 中的架构

<?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 中的绑定(bind)文件

<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 执行它时,演示类会产生以下输出

<?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?)生成以下内容

<?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 上运行?

更多信息:

maven-jaxb2-plugin 与其他类一起生成以下类

@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) 就像下面的代码一样,那么它就可以工作

@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?如果有帮助,此链接有 zipped Maven Project

最佳答案

尝试添加 xjc:javaType定制。

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

关于java - XmlAdapter 无法与较新版本的 JAXB 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25942081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com