wfg4
wfg4

Reputation: 197

JAXB inner element ignored

I have some XML that I am processing using JAXB. I don't understand why I cannot access the inner element of this tag.

XML snippet:

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

From this I generated an 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>

I am using maven-jaxb2-plugin and all works well:

<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>

On my object, I have methods for getSource() and getVersion() but no getValue() or something along those lines. Am I fundamentally missing something? Is attempting to access inner element data in this manner not correct?

EDIT: Included generated Java code

/**
         * <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;
            }

        }

Upvotes: 1

Views: 524

Answers (1)

bdoughan
bdoughan

Reputation: 149017

The schema definition of the payload element should look like the following to get the JAXB class you are looking for. You will need to fix how you are generating the XML schema from your XML document.

<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>

For More Information

Upvotes: 1

Related Questions