解析异常

时间:2013-08-26 21:06:24

标签: maven jaxb

我在xml的两个不同复杂类型中有相同的元素id,如果我尝试使用maven Jaxb插件进行解析,我会得到以下异常,是否有一种方法可以解析而无需使用绑定帮助重命名元素,因为我在架构中至少有30次属性id。提前谢谢

com.sun.istack.SAXParseException2:已定义属性“Id”。使用< jaxb:property>解决这个冲突。

            <xs:element name="aliases" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="alias" minOccurs="0" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element ref="tran" minOccurs="0"/>
                                    <xs:element name="id">
                                        <xs:complexType>
                                            <xs:simpleContent>
                                                <xs:extension base="xs:string">
                                                    <xs:attribute name="old_value" type="xs:string"/>
                                                </xs:extension>
                                            </xs:simpleContent>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:sequence>
                                <xs:attribute name="end" type="xs:string"/>
                                <xs:attribute name="start" type="xs:string"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>



<xs:element name="tin_affiliation">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="tran" minOccurs="0"/>
            <xs:element name="id">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="old_value" type="xs:string"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="end" type="xs:string"/>
        <xs:attribute name="start" type="xs:string"/>
    </xs:complexType>
</xs:element>

3 个答案:

答案 0 :(得分:1)

使用绑定文件的另一种解决方案是单独声明ID类型。因为它在两个元素中看起来都相同,并且因为在这种情况下的冲突是由于重复的类型声明。

然后,Schema将如下所示。

注意:它经过测试和运行,但由于我没有定义,因此必须发表评论<xs:element ref="tran" minOccurs="0"/>

模式

<xs:element name="aliases">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="alias" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <!--<xs:element ref="tran" minOccurs="0"/> -->
                        <xs:element name="id" type="idType" />
                    </xs:sequence>
                    <xs:attribute name="end" type="xs:string"/>
                    <xs:attribute name="start" type="xs:string"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="tin_affiliation">
    <xs:complexType>
        <xs:sequence>
            <!-- <xs:element ref="tran" minOccurs="0"/> -->
            <xs:element name="id" type="idType" />
        </xs:sequence>
        <xs:attribute name="end" type="xs:string"/>
        <xs:attribute name="start" type="xs:string"/>
    </xs:complexType>
</xs:element>

<xs:complexType name="idType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="old_value" type="xs:string"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

生成的Java类:IdType

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "idType", propOrder = {
    "value"
})
public class IdType {

    @XmlValue
    protected String value;
    @XmlAttribute(name = "old_value")
    protected String oldValue;

    /**
     * Gets the value of the value property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getValue() {
        return value;
    }

    /**
     * Sets the value of the value property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * Gets the value of the oldValue property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getOldValue() {
        return oldValue;
    }

    /**
     * Sets the value of the oldValue property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setOldValue(String value) {
        this.oldValue = value;
    }

}

生成的Java类:TinAffiliation

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id"
})
@XmlRootElement(name = "tin_affiliation")
public class TinAffiliation {

    @XmlElement(required = true)
    protected IdType id;
    @XmlAttribute(name = "end")
    protected String end;
    @XmlAttribute(name = "start")
    protected String start;

    /**
     * Gets the value of the id property.
     * 
     * @return
     *     possible object is
     *     {@link IdType }
     *     
     */
    public IdType getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     * @param value
     *     allowed object is
     *     {@link IdType }
     *     
     */
    public void setId(IdType value) {
        this.id = value;
    }

    /**
     * Gets the value of the end property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getEnd() {
        return end;
    }

    /**
     * Sets the value of the end property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setEnd(String value) {
        this.end = value;
    }

    /**
     * Gets the value of the start property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getStart() {
        return start;
    }

    /**
     * Sets the value of the start property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setStart(String value) {
        this.start = value;
    }

}

答案 1 :(得分:0)

XML元素中可以有多个具有相同名称的XML属性/元素,没有任何问题。您看到的异常是,这会导致生成的类具有两个具有相同名称的属性。在以下情况下可能会发生这种情况。

  1. 复杂类型包含属性和具有相同名称的元素。
  2. 复杂类型包含与属性/元素同名的属性/元素,作为其扩展的复杂类型。

答案 2 :(得分:0)

我能够使用插件org.codehaus.mojo实现这一点:jaxb2-maven-plugin:2.3和org.jvnet.jaxb2_commons:jaxb2-basics-annotate:0.6.4并且具有以下绑定配置,特别是多个= “真

<jxb:bindings node=".//xs:attribute[@name='abc]"
                multiple="true">
                <jxb:property name="abc_attribute" />
            </jxb:bindings>
        </jxb:bindings>