JAXB - XML Schema Types, Binary Data

时间:2023-03-08 22:45:55

Data that has no "natural" representation with printable characters must, for inclusion in an XML file, still be represented in printable characters. The simple technique for this consists in converting the binary byte values to their hexadecimal representations. The XML Schema datatype to use in this case is xsd:hexBinary. A sample schema declaration is shown below.

<xsd:complexType name="BinaryType">
<xsd:sequence>
<xsd:element name="data" type="xsd:hexBinary"/>
</xsd:sequence>
</xsd:complexType>

The Java class produced by JAXB contains a convenient pair of getter and setter methods for accessing the instance variable (called data) that stores the binary data. Its type and the type for passing the binary data is byte[]. All conversions are handled by JAXB.

public class BinaryType {

    protected byte[] data;

    public byte[] getData() {
return data;
} public void setData(byte[] value) {
this.data = ((byte[]) value);
}
}