AKB
AKB

Reputation: 5938

xsd to jaxb adds special character

I have xsd file as given below:

<xs:complexType name="sampleTest">
        <xs:sequence>
            <xs:element name="testName" type="xs:string"/>
            <xs:element name="actualData" type="tns:actualData"/>
            <xs:element name="expectedDataAPI" type="xs:string"/>
            <xs:element name="actualDataPostStatus" type="xs:string" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

by using JAXB xjc plugin, it generates proper bean class. But, if I add minOccurance and maxOccurance to an element it adds a special character.

<xs:element name="actualData" type="tns:actualData" minOccurs="1" maxOccurs="unbounded"/>

i.e it generates

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "sampleTest", propOrder = {
        "testName",
        "actualDatas",
        "expectedDataAPI",
        "actualDataPostStatus"
    })

    ....
    ...
        @XmlElement(name = "actualData", required = true)
        protected List<ActualData> actualDatas;
...
...
public List<ActualData> getActualDatas() {
        if (actualDatas == null) {
            actualDatas = new ArrayList<ActualData>();
        }
        return this.actualDatas;
    }

instead of actualData, it become actualDatas. Is there any filter is required? what changes I need to use to avoid special character.

I am using Maven to generate jaxb with xjc plugin in eclipse. I am using Java 1.6 and Jaxb 2.2.7

Update

I have binding as:

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

    <jaxb:globalBindings  xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <jaxb:serializable uid="1"/>
        <xjc:simple />
    </jaxb:globalBindings>

</jaxb:bindings>

Upvotes: 1

Views: 1065

Answers (1)

bdoughan
bdoughan

Reputation: 149017

You must be including an extension (potentially accidentally) to cause this behaviour. My guess is xjc:simple which will cause the plural name to be generated for repeated elements.


Standard JAXB Behaviour

Below is an example showing standard JAXB behaviour.

XML Schema (schema.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema" 
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">

    <xs:complexType name="sampleTest">
        <xs:sequence>
            <xs:element name="actualData" type="tns:actualData" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="actualData"/>

</xs:schema>

XJC Call

xjc schema.xsd

Generated Class (SampleTest)

The generated field and property are singular actualData.

package org.example.schema;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sampleTest", propOrder = {"actualData"})
public class SampleTest {

    @XmlElement(required = true)
    protected List<ActualData> actualData;

    public List<ActualData> getActualData() {
        if (actualData == null) {
            actualData = new ArrayList<ActualData>();
        }
        return this.actualData;
    }

}

Using Your Bindings File

Bindings File (binding.xml)

This is the bindings file from your question.

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

    <jaxb:globalBindings  xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <jaxb:serializable uid="1"/>
        <xjc:simple />
    </jaxb:globalBindings>

</jaxb:bindings>

XJC Call

xjc -extension -b binding.xml schema.xsd

Generated Class (SampleTest)

The generated field and property are not singular actualDatas.

package org.example.schema;

import java.io.Serializable;
import java.util.*;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sampleTest", propOrder = {"actualDatas"})
public class SampleTest
    implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElement(name = "actualData", required = true)
    protected List<ActualData> actualDatas;

    public List<ActualData> getActualDatas() {
        if (actualDatas == null) {
            actualDatas = new ArrayList<ActualData>();
        }
        return this.actualDatas;
    }

}

Upvotes: 2

Related Questions