If you are generating your model from XML Schema, below is an approach you can use to cause an XmlAdapter
to be generated for your use case.
Formatter Object
You will need to create a class with methods that can convert the string to/from BigDecimal
.
public class BigDecimalFormatter {
public static String printBigDecimal(BigDecimal value) {
// TODO - Conversion logic
}
public static BigDecimal parseBigDecimal(String value) {
// TODO - Conversion logic
}
}
External Bindings Document (bindings.xml)
An external binding document is used to specify that your custom conversion class should be used when converting the XML string to/from your BigDecimal
property.
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="//xs:element[@name='rate']">
<jxb:property>
<jxb:baseType>
<jxb:javaType name="java.math.BigDecimal"
parseMethod="forum20711223.BigDecimalFormatter.parseBigDecimal" printMethod="forum20711223.BigDecimalFormatter.printBigDecimal" />
</jxb:baseType>
</jxb:property>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
XJC Call
The -b
flag is used to reference the external binding document.
xjc -b binding.xml schema.xsd
Full Example