开发者

Jaxb - Namespace problems with SchemaGen and java.util.Map

When a class contains a java.util.Map, SchemaGen generates an xsd with a namespace on the Map elements. However when the marshaller generates xml from the same class no namespace is present.

This may be solvable with XmlAdapter however that seems to require the creation of 3 extra classes per Map. As I have a large number of maps I don't want to go down this route.

Is there any way to get SchemaGen and the Marshaller to generate the same xml?

Example Classes and schemas:

Test1:

@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;
    }
}

Test2:

public class Test2 implements Serializable {

    private String name;
    private String value;

    public Test2() {
    }

    public Test2(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    开发者_运维问答public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

SchemaGen output:

<?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 that conforms to the schema. (note. jaxb will not unmarshall this xml!)

<?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 that Jaxb produces. (note. It does not conform to the schema!)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜