【问题标题】:XSD generate a MAP<String, Boolean> propertiesXSD 生成一个 MAP<String, Boolean> 属性
【发布时间】:2013-10-31 16:47:12
【问题描述】:

我正在尝试从包含Map&lt;String, Boolean 的 XSD 文件生成 java 类。我读过教程 (http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html),它说我必须使用适配器和绑定才能达到预期的效果。

但由于某种原因,生成后的属性是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>

我的绑定文件:

<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>

【问题讨论】:

  • 如果您发布收到的错误消息可能会有所帮助。如果您从将返回 List 的方法中请求 Map,则您的代码应该无法编译。
  • 这可能是一百万件事。确保绑定文件以 .xjb 结尾。尝试手动运行 jaxb 生成器,看看是否会收到更好的错误消息。使用其他工具测试 xpath 表达式。
  • 我最初的猜测是绑定目录不正确。也许您应该改用“bindingFiles”。你解决问题了吗?

标签: java xml maven jaxb xsd


【解决方案1】:

从 StringBooleanMap 后面删除 &lt;String, Boolean&gt;,它不会做你认为它正在做的事情。 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" />

【讨论】:

    猜你喜欢
    • 2011-04-07
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多