java - JAXB 不会按预期解析 .xsd。 C# 从同一文件自动生成

标签 java xml jaxb xsd

我对 JAXB 当前从 .xsd 文件生成 java 对象的方式遇到了一些问题。下面是我正在使用的 .xsd 文件中的代码片段。此代码的目的是它将有一个 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 却不会。

最佳答案

对于 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 模式的完全可接受的表示。

关于java - JAXB 不会按预期解析 .xsd。 C# 从同一文件自动生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16742222/

相关文章:

java - 如何使用 JDBC 高效导出/导入数据库数据

toString() 的 Java 值不打印

iphone - 我应该如何在 iOS 中动态创建表单?

c# - 在 xml 中获取属性名称和属性值

plugins - 如何使用 Maven 中的 jaxb_commons 插件

java - 为 oxm jaxb2 spring 服务获取 406。编码请求体有效,返回响应体无效

java - 使用 Mockito 调用验证 super.method()

java - 为什么我们需要上下文类加载器

php - Ajax 和 Internet Explorer

java - JAXB - 生成示例 xml?