Here is my configuration, which i use to generate classes.
test.xsd - your xsd file
<?xml version="1.0" encoding="utf-8"?> <xsd:schema elementFormDefault="qualified" targetNamespace="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:simpleType name="DoublePositive"> <xsd:restriction base="xsd:double"> <xsd:minExclusive value="0"/> </xsd:restriction> </xsd:simpleType> <xsd:complexType name="foo"> <xsd:attribute name="bar" type="DoublePositive"/> </xsd:complexType> </xsd:schema>
text.xml - file with global bindings.
<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <jaxb:globalBindings generateElementProperty="false"> <jaxb:javaType name="java.util.Double" xmlType="DoublePositive" parseMethod="Converter.fromString" printMethod="Converter.toString"/> </jaxb:globalBindings> </jaxb:bindings>
Converter.java - class with conversion functions.
public class Converter { public static Double fromString(String str){ return Double.parseDouble(str); } public static String toString(Double value){ return String.valueOf(value); } }
That's all. Call xjc with command
xjc -b test.xml -classpath . test.xsd
.
$ xjc -version xjc version "JAXB 2.1.10" JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build JAXB 2.1.10) $ xjc -b test.xml -classpath . test.xsd parsing a schema... [WARNING] EmptyTargetNamespace: In schema document 'file:/pwd/test.xsd', the value of the 'targetNamespace' attribute cannot be an empty string. line 3 of file:/pwd/test.xsd compiling a schema... generated/Adapter1.java generated/Foo.java generated/ObjectFactory.java
and generated file content:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo")
public class Foo {
@XmlAttribute
@XmlJavaTypeAdapter(Adapter1 .class)
protected Double bar;
/**
* Gets the value of the bar property.
*
* @return
* possible object is
* {@link String }
*
*/
public Double getBar() {
return bar;
}
/**
* Sets the value of the bar property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setBar(Double value) {
this.bar = value;
}
}
and Adapter class
package generated;
import java.util.Double;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class Adapter1
extends XmlAdapter<String, Double>
{
public Double unmarshal(String value) {
return (Converter.fromString(value));
}
public String marshal(Double value) {
return (Converter.toString(value));
}
}
So, as you can see, xjc generates the classes correctly. If you still get a String
, then maybe you forgot to add something in your configuration. But without the details about your configuration, it is impossible to say what is wrong with your code.