gpt4 book ai didi

java - XSD 生成一个 MAP 属性

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:54 25 4
gpt4 key购买 nike

我正在尝试从包含 Map<String, Boolean 的 XSD 文件生成 java 类.我读过教程 ( http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html ),其中说我必须使用 Adapter 和 Binding 才能获得预期的结果。

但由于某种原因,生成后的属性是 List而不是 Map .你能帮我找出我的错误吗?

谢谢!

我的 XSD 架构:

<xs:element name="AutoCompleteReq">
<xs:complexType>
<xs:sequence>
<xs:element name="queryString" type="xs:string"/>
<xs:element name="boostingFactors" type="test:StringBooleanMapModeller" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="StringBooleanMapModeller">
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="key" type="xs:string"/>
<xs:element name="value" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

我的绑定(bind)文件:

<jaxb:bindings schemaLocation="AutoCompleteReq.xsd">

<jaxb:bindings node="//xs:element[@name='AutoCompleteReq']//xs:element[@name='boostingFactors']">
<jaxb:property>
<jaxb:baseType name="com.company.support.jaxb.StringBooleanMap&lt;String,Boolean&gt;" />
</jaxb:property>
</jaxb:bindings>

</jaxb:bindings>

</jaxb:bindings>

StringBooleanMap.java

package com.company.support.jaxb;

import java.util.HashMap;

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

@XmlJavaTypeAdapter(StringBooleanMapAdapter.class)
public class StringBooleanMap<String, Boolean> extends HashMap<String, Boolean> {

}

StringBooleanMapAdapter.java

package com.company.support.jaxb;

import java.util.HashMap;
import java.util.Map;

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

import com.company.xml.representation.StringBooleanMapModeller;

public class StringBooleanMapAdapter extends XmlAdapter<StringBooleanMapModeller, HashMap<String, Boolean>> {

@Override
public HashMap<String, Boolean> unmarshal(StringBooleanMapModeller v)
throws Exception {

HashMap<String, Boolean> map = new HashMap<String, Boolean>();
for(StringBooleanMapModeller.Entry e : v.getEntry())
{
map.put(e.getKey(), e.isValue());
}

return map;
}

@Override
public StringBooleanMapModeller marshal(HashMap<String, Boolean> v)
throws Exception {

StringBooleanMapModeller modeller = new StringBooleanMapModeller();
for(Map.Entry<String, Boolean> entry : v.entrySet())
{
StringBooleanMapModeller.Entry e = new StringBooleanMapModeller.Entry();
e.setKey(entry.getKey());
e.setValue(entry.getValue());
modeller.getEntry().add(e);
}
return modeller;
}
}

pom.xml(XSD 生成部分)

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>Representations</id>
<configuration>
<schemaDirectory>XSD/Representation</schemaDirectory>
<packageName>com.company.xml.representation</packageName>
<bindingDirectory>XSD/Representation</bindingDirectory>
<outputDirectory>src/main/generated-sources</outputDirectory>
<staleFile>${project.build.directory}/generated-sources/jaxb/.representation</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
<goals>
<goal>xjc</goal>
</goals>
</execution>
<execution>
<id>Events</id>
<configuration>
<schemaDirectory>XSD/Representation/Event</schemaDirectory>
<packageName>com.company.xml.representation.event</packageName>
<bindingDirectory>XSD/Representation/Event</bindingDirectory>
<outputDirectory>src/main/generated-sources</outputDirectory>
<staleFile>${project.build.directory}/generated-sources/jaxb/.event</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
</plugin>

最佳答案

删除 <String, Boolean>在 StringBooleanMap 后面,它并没有按照您的想法进行操作。 String 和 Boolean 被解释为通用参数,而不是 java.lang.String 和 java.lang.Boolean。

正确:

package com.company.support.jaxb;

import java.util.HashMap;

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

@XmlJavaTypeAdapter(StringBooleanMapAdapter.class)
public class StringBooleanMap extends HashMap<String, Boolean> {

}

您的代码被解释为:

public class StringBooleanMap<S,B> extends HashMap<S,B> {

此外,将 jaxb:baseType 引用更改为:

<jaxb:baseType name="com.company.support.jaxb.StringBooleanMap" />

关于java - XSD 生成一个 MAP<String, Boolean> 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524240/

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