【问题标题】:JAXB inner element ignoredJAXB 内部元素被忽略
【发布时间】:2014-01-13 20:24:51
【问题描述】:

我有一些正在使用 JAXB 处理的 XML。我不明白为什么我不能访问这个标签的内部元素。

XML sn-p:

<binary>
    <route>
        <payload source="SomeService" version="1.2"><![CDATA[ACAA8f///...snip...AAAAAGGAAAARAFR]]>
        </payload>
    </route>
</binary>

由此我生成了一个 XSD:

 <xsd:element name="binary">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="route">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="payload">
                  <xsd:complexType>
                    <xsd:attribute name="source" type="xsd:string" />
                    <xsd:attribute name="version" type="xsd:string" />
                  </xsd:complexType>
                </xsd:element>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

我正在使用 maven-jaxb2-plugin 并且一切正常:

<build>
    <plugins>
      <plugin>
        <inherited>true</inherited>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

在我的对象上,我有 getSource() 和 getVersion() 的方法,但没有 getValue() 或类似的东西。我从根本上错过了什么吗?尝试以这种方式访问​​内部元素数据不正确吗?

编辑:包含生成的 Java 代码

/**
         * <p>Java class for anonymous complex type.
         * 
         * <p>The following schema fragment specifies the expected content contained within this class.
         * 
         * <pre>
         * &lt;complexType>
         *   &lt;complexContent>
         *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
         *       &lt;attribute name="source" type="{http://www.w3.org/2001/XMLSchema}string" />
         *       &lt;attribute name="version" type="{http://www.w3.org/2001/XMLSchema}string" />
         *     &lt;/restriction>
         *   &lt;/complexContent>
         * &lt;/complexType>
         * </pre>
         * 
         * 
         */
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "")
        public static class Payload {

            @XmlAttribute(name = "source")
            protected String source;
            @XmlAttribute(name = "version")
            protected String version;
            @XmlValue
            protected String value;
            /**
             * Gets the value of the source property.
             * 
             * @return
             *     possible object is
             *     {@link String }
             *     
             */
            public String getSource() {
                return source;
            }

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

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

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

        }

【问题讨论】:

  • 添加您创建的 Java 代码以及代码遇到的问题。
  • 问题在于 Solution.Binary.Route.Payload 对象包含属性方法,但不包含内部元素数据(我希望有一种方法可以检索 CDATA 字符串数据)。

标签: java xml maven jaxb


【解决方案1】:

payload 元素的架构定义应如下所示,以获取您正在寻找的 JAXB 类。您需要修正从 XML 文档生成 XML 模式的方式。

<element name="payload">
    <complexType>
        <simpleContent>
            <extension base="string">
                <xsd:attribute name="source" type="xsd:string" />
                <xsd:attribute name="version" type="xsd:string" />
            </extension>
        </simpleContent>
    </complexType>
</element>

更多信息

【讨论】:

  • 感谢您的澄清。
猜你喜欢
  • 1970-01-01
  • 2011-06-29
  • 2021-11-20
  • 2011-12-12
  • 2012-11-06
  • 2011-01-25
  • 2011-09-28
  • 2012-10-12
  • 1970-01-01
相关资源
最近更新 更多