1

我在 JAXB 当前从 .xsd 文件生成 java 对象的方式上遇到了一些问题。下面我有一个来自我正在使用的 .xsd 文件的代码片段。这段代码的目的是它会有一个LogicalDevices 列表,这些LogicalDevices 是包含各种信息的对象。

                        <xs:element name="LogicalDeviceList">
                            <xs:annotation>
                                <xs:documentation>List of all LogicalDevices currently added for the application</xs:documentation>
                            </xs:annotation>
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="LogicalDevice" minOccurs="0" maxOccurs="unbounded">
                                        <xs:annotation>
                                            <xs:documentation>An added logical device</xs:documentation>
                                        </xs:annotation>
                                        <xs:complexType>
                                            <xs:attribute name="DeviceDefinitionId" use="required">
                                                <xs:annotation>
                                                    <xs:documentation>The DeviceDefinitionId of the Logical device</xs:documentation>
  .......... Other LogicalDevice Information

目前,JAXB 解析器创建一个对象,其中 LogicalDeviceList 不是一个 LogicalDevices 列表,而 LogicDevice 返回一个 DeviceDefinitionIds 列表。

由于我接收和解组的 XML 无论如何都不能改变,有没有办法解决这个问题?是否像将 .xsd 文件更改为这样读取一样简单

更新:下面的修改不起作用。2013 年 5 月 24 日

                        <xs:element name="LogicalDeviceList" minOccurs="0" maxOccurs="unbounded">
                            <xs:annotation>
                                <xs:documentation>List of all LogicalDevices currently added for the application</xs:documentation>
                            </xs:annotation>
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="LogicalDevice">
                                        <xs:annotation>
                                            <xs:documentation>An added logical device</xs:documentation>
                                        </xs:annotation>
                                        <xs:complexType>
                                            <xs:attribute name="DeviceDefinitionId" use="required">
                                                <xs:annotation>
                                                    <xs:documentation>The DeviceDefinitionId of the Logical device</xs:documentation>

如果是这样,为什么 C# .xsd 解析器会按预期从原始 xsd 生成对象和列表,而 JAXB 不会。

4

1 回答 1

0

对于 XML 模式片段:

<xs:element name="LogicalDeviceList">
    <xs:annotation>
        <xs:documentation>List of all LogicalDevices currently added for the application</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="LogicalDevice" minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>An added logical device</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                    <xs:attribute name="DeviceDefinitionId" use="required">
                        <xs:annotation>
                            <xs:documentation>The DeviceDefinitionId of the Logical device</xs:documentation>
                        ...   

您将获得如下所示的类结构,其中LogicalDeviceList该类具有一组LogicalDevice实例。

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "logicalDevice"
    })
    public static class LogicalDeviceList {

        @XmlElement(name = "LogicalDevice")
        protected List<LogicalDeviceList.LogicalDevice> logicalDevice;

JAXB 可能与 C# 生成的不完全匹配,但它是 XML 模式的完全可接受的表示。

于 2013-05-29T20:02:19.627 回答