0

We have a requirement where XML tags are not similar to java POJO attributes.

We need a solution to map XML tags to POJO with different name.

Here is the same XML,

<RES>
     <TAG1>
       value
     </TAG1>
</RES>

Here is the POJO class,

public class Response {
    protected String tag1Value;
...}

Here I want to map to tag1Value in POJO.

I have found below syntax can be used to map XML to java attributes.

public class Response {
     @XmlElement(name="TAG1")
    protected String tag1Value;
...}

But I want to know how to configure this in XSD, so that POJO can be generated with name attribute in XMLElement.

With the below XSD how to specify java attribute name and XML nae together,

<xsd:element name="tag1Value" minOccurs="0" maxOccurs="1">

Help appreciated...

2 Answers2

0

You can do it by customizing jaxb bindings for your specified schema:

(I am making assumptions on how your xsd looks, but you can adapt the path to your target node if I am wrong)

<jxb:bindings schemaLocation="../xsd/your_schema.xsd">
        <jxb:bindings node="//xs:element[@name='Response']//xs:complexType//xs:all//xs:element[@name='tag1Value']">
            <annox:annotate target="field">
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlElement" name="TAG1" />
            </annox:annotate>
        </jxb:bindings>
</jxb:bindings>
martidis
  • 2,897
  • 1
  • 11
  • 13
  • 2
    Also have a look at [JAXB User Guide: 2. Customization of Schema Compilation](https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#customization-of-schema-compilation) –  Feb 15 '18 at 13:38
  • You get it the other way round. Apparenty, the element in the schema is `TAG1`, not `tag1Value`. So you shouldn't customize the property annotation to be `@XmlElement(name="TAG1")`, this will be generated by default. You should customize the property to be named `tag1Value`. Which can be done with built-in bindings (`jaxb:property`). – lexicore Feb 15 '18 at 16:14
0

This is what bindings are for.

Create a file named something like bindings.xjb (xjb is file extenstion by convention). In this file you can customize certain aspects of code generation. For instance, you can use jaxb:property to specify the name of the property in the generated class:

<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    jaxb:extensionBindingPrefixes="xjc">

    <jaxb:bindings 
        schemaLocation="mySchema.xsd" 
        node="/xs:schema">

        <!-- node is the XPath leading to the element you want to customize -->
        <jaxb:bindings node="xs:complexType[@name='SomeComplexType']/xs:sequence/xs:element[@name='TAG1']">
            <jaxb:property name="tag1Value"/>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

You can provide this file during compilation with xjc mySchema.xsd -b bindings.xjb or using configuration options of Maven plugins or Ant tasks (whatever you use).

This will generate property named tag1Value which will be annotated with @XmlElement(name="TAG1").

lexicore
  • 42,748
  • 17
  • 132
  • 221