Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jaxb—SchemaGen和java.util.Map的命名空间问题_Jaxb_Jaxb2 - Fatal编程技术网

Jaxb—SchemaGen和java.util.Map的命名空间问题

Jaxb—SchemaGen和java.util.Map的命名空间问题,jaxb,jaxb2,Jaxb,Jaxb2,当一个类包含java.util.Map时,SchemaGen会在映射元素上生成一个带有名称空间的xsd。但是,当封送拆收器从同一个类生成xml时,没有名称空间 这可以通过XmlAdapter解决,但是似乎需要为每个映射创建3个额外的类。因为我有很多地图,所以我不想走这条路 有没有办法让SchemaGen和Marshaller生成相同的xml 示例类和模式: 测试1: @XmlRootElement public class Test1 implements Serializable {

当一个类包含java.util.Map时,SchemaGen会在映射元素上生成一个带有名称空间的xsd。但是,当封送拆收器从同一个类生成xml时,没有名称空间

这可以通过XmlAdapter解决,但是似乎需要为每个映射创建3个额外的类。因为我有很多地图,所以我不想走这条路

有没有办法让SchemaGen和Marshaller生成相同的xml

示例类和模式:

测试1:

@XmlRootElement
public class Test1 implements Serializable {

    private String field1;

    private Map<String, Test2> attributes;

    public Test1() {}

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public Map<String, Test2> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, Test2> attributes) {
        this.attributes = attributes;
    }
}
SchemaGen输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://testns" xmlns:tns="http://testns" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="test1" type="tns:test1"/>

  <xs:complexType name="test1">
    <xs:sequence>
      <xs:element name="attributes">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="key" minOccurs="0" type="xs:string"/>
                  <xs:element name="value" minOccurs="0" type="tns:test2"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="field1" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="test2">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="value" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

符合模式的Xml。(注意。jaxb不会对该xml进行解组!)


a1
a1
v1
f1
Jaxb生成的Xml。(注意,它不符合架构!)


a1
a1
v1
f1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://testns" xmlns:tns="http://testns" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="test1" type="tns:test1"/>

  <xs:complexType name="test1">
    <xs:sequence>
      <xs:element name="attributes">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="key" minOccurs="0" type="xs:string"/>
                  <xs:element name="value" minOccurs="0" type="tns:test2"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="field1" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="test2">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="value" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<ns1:test1 
    xmlns:ns1="http://testns" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xsi:schemaLocation="http://testns ../build/schemas/schema1.xsd"
    >
    <ns1:attributes>
        <ns1:entry>
            <ns1:key>a1</ns1:key>
            <ns1:value>
                <ns1:name>a1</ns1:name>
                <ns1:value>v1</ns1:value>
            </ns1:value>
        </ns1:entry>
    </ns1:attributes>
    <ns1:field1>f1</ns1:field1>
</ns1:test1>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:test1 xmlns:ns2="http://testns">
    <ns2:attributes>
        <entry>
            <key>a1</key>
            <value>
                <ns2:name>a1</ns2:name>
                <ns2:value>v1</ns2:value>
            </value>
        </entry>
    </ns2:attributes>
    <ns2:field1>f1</ns2:field1>
</ns2:test1>