File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes XMLBeans versus JAXB schema compilation differences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "XMLBeans versus JAXB schema compilation differences" Watch "XMLBeans versus JAXB schema compilation differences" New topic
Author

XMLBeans versus JAXB schema compilation differences

John Zimmermann
Greenhorn

Joined: Jun 27, 2012
Posts: 1
Hi,

I have an XML fragment that I am trying to compile using JAXB. The compilation executes successfully but there are some differences between the java files generated by JAXB when compared to XMLBeans. Here is the XML fragment:

<simpleType name="MyDataType">
<restriction base="xsd:string">
<enumeration value="Text"/><!-- enum const = 0 -->
<enumeration value="Integer"/><!-- enum const = 1 -->
<enumeration value="Float"/><!-- enum const = 2 -->
<enumeration value="Boolean"/><!-- enum const = 3 -->
<enumeration value="TimeStamp"/><!-- enum const = 4 -->
<enumeration value="Code"/><!-- enum const = 5 -->
<enumeration value="MultiLanguageText"/><!-- enum const = 6 -->
</restriction>
</simpleType>

JAXB will generate the following class:

@XmlType(name = "MyDataType", namespace = "http://www.example.com/aSampleNamespace")
@XmlEnum
public enum MyDataType{

@XmlEnumValue("Text")
TEXT("Text"),
@XmlEnumValue("Integer")
INTEGER("Integer"),
@XmlEnumValue("Float")
FLOAT("Float"),
@XmlEnumValue("Boolean")
BOOLEAN("Boolean"),
@XmlEnumValue("TimeStamp")
TIME_STAMP("TimeStamp"),
@XmlEnumValue("Code")
CODE("Code"),
@XmlEnumValue("MultiLanguageText")
MULTI_LANGUAGE_TEXT("MultiLanguageText");
private final String value;

MyDataType(String v) {
value = v;
}

public String value() {
return value;
}

public static MyDataType fromValue(String v) {
for (MyDataType c: MyDataType.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}

XMLBeans would apply an ordinal to the elements like so:

public interface FlexFieldDataType extends org.apache.xmlbeans.XmlString
{
public static final org.apache.xmlbeans.SchemaType type = (org.apache.xmlbeans.SchemaType)
org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(FlexFieldDataType.class.getClassLoader(), "schemaorg_apache_xmlbeans.system.s463C563221D006C73594BDAF6D050EA7").resolveHandle("flexfielddatatypeb902type");

org.apache.xmlbeans.StringEnumAbstractBase enumValue();
void set(org.apache.xmlbeans.StringEnumAbstractBase e);

static final Enum TEXT = Enum.forString("Text");
static final Enum INTEGER = Enum.forString("Integer");
static final Enum FLOAT = Enum.forString("Float");
static final Enum BOOLEAN = Enum.forString("Boolean");
static final Enum TIME_STAMP = Enum.forString("TimeStamp");
static final Enum CODE = Enum.forString("Code");
static final Enum MULTI_LANGUAGE_TEXT = Enum.forString("MultiLanguageText");

static final int INT_TEXT = Enum.INT_TEXT;
static final int INT_INTEGER = Enum.INT_INTEGER;
static final int INT_FLOAT = Enum.INT_FLOAT;
static final int INT_BOOLEAN = Enum.INT_BOOLEAN;
static final int INT_TIME_STAMP = Enum.INT_TIME_STAMP;
static final int INT_CODE = Enum.INT_CODE;
static final int INT_MULTI_LANGUAGE_TEXT = Enum.INT_MULTI_LANGUAGE_TEXT;

/**
* Enumeration value class for com.sumtotalsystems.sumtotal7.sumtotalbo.FlexFieldDataType.
* These enum values can be used as follows:
* <pre>
* enum.toString(); // returns the string value of the enum
* enum.intValue(); // returns an int value, useful for switches
* // e.g., case Enum.INT_TEXT
* Enum.forString(s); // returns the enum value for a string
* Enum.forInt(i); // returns the enum value for an int
* </pre>
* Enumeration objects are immutable singleton objects that
* can be compared using == object equality. They have no
* public constructor. See the constants defined within this
* class for all the valid values.
*/
static final class Enum extends org.apache.xmlbeans.StringEnumAbstractBase
{
/**
* Returns the enum value for a string, or null if none.
*/
public static Enum forString(java.lang.String s)
{ return (Enum)table.forString(s); }
/**
* Returns the enum value corresponding to an int, or null if none.
*/
public static Enum forInt(int i)
{ return (Enum)table.forInt(i); }

private Enum(java.lang.String s, int i)
{ super(s, i); }

static final int INT_TEXT = 1;
static final int INT_INTEGER = 2;
static final int INT_FLOAT = 3;
static final int INT_BOOLEAN = 4;
static final int INT_TIME_STAMP = 5;
static final int INT_CODE = 6;
static final int INT_MULTI_LANGUAGE_TEXT = 7;

Is there anyway I can achieve this behavior using JAXB? The ordinals are references throughout the client code and it would be nice to preserve this behavior ...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: XMLBeans versus JAXB schema compilation differences