【问题标题】:how declare xml-element in xsd-schema如何在 xsd-schema 中声明 xml-element
【发布时间】:2013-07-25 00:43:15
【问题描述】:

我有带有元素的 xml:

<attribute name="attributeName1" 
           type="typeName1">value1</attribute>
...
<attribute name="attributeName2" 
           type="typeName2">
    <row order="1">
        <attribute name="attributeName3" 
                   type="typeName3">value3</attribute>
        ...
    </row>
    ...
 </attribute>

如何在 xsd-schema 中声明这些元素?

【问题讨论】:

    标签: xml jaxb xsd


    【解决方案1】:

    您可以定义attribute 元素,使其具有混合内容的类型。像下面这样:

    <xsd:element name="attribute">
        <xsd:complexType mixed="true">
            <xsd:sequence>
                <xsd:element name="row" minOccurs="0" maxOccurs="unbounded">
                    <xsd:complexType>
                        ...
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    

    这将为您提供一个类似于以下内容的 JAXB 类,您最终会得到一个属性,该属性将存储文本和 Row 对象。

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "content"
    })
    @XmlRootElement(name = "attribute")
    public class Attribute {
    
        @XmlElementRef(name = "row", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
        @XmlMixed
        protected List<Serializable> content;
    
    }
    

    如果您改为执行以下操作,从长远来看,您可能会更快乐。

    <attribute name="attributeName1" type="typeName1">
        <value>value1</value>
    </attribute>
    

    【讨论】:

    • 感谢您的快速回复:)
    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2014-06-17
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多