我正在使用以下来源执行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中的绑定文件
<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<String,String>" />
</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产生以下输出
<?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
答案 0 :(得分:0)
尝试添加xjc:javaType自定义。
<xjc:javaType name="org.acme.foo.PropertiesMap"
adapter="org.acme.foo.PropertiesMapAdapter"/>