hudi
hudi

Reputation: 16555

Problem with unmarshaling xsd:base64Binary

JAXB generated from XSD base64Binary and hexBinary to byte array. I have xsd element with base64Binary. This is strict and cannot be changed. But in this element we has value of hexBinary (yea a little bit confused). And now where it is problem. I dont dont how is JAXB unmarshaling these element but there is something not sutaible to me. So if there could be hexBinary in my element it unmarshaling right. But im my case element has type base64Binary and JAXB unmarshaling these element something else what is wrong to me. Is there a way to change how JAXB handle with these element and tell him to handle as these element would be hexBinary. maybe confusing question but I need help with these.

so in shortly way I have element base64Binary but I wanna to tell JAXB to unmarshaling as these will be hexBinary

I read something that I have to create new XmlAdapter but I dont know where should I use it because I have just classes which generated JAXB

UPDATE:

I found where is a problem. when element is base64Binary in generated class it created this

XmlElement(name="name") protected byte[] element

but when it is hexBinary it create

XmlJavaTypeAdapter(HexBinaryAdapter.class) XmlElement(name="name") protected byte[] element

so now I need to know how to say jaxb to use these adapter when I have base64Binary
protected byte[] element

Upvotes: 2

Views: 3912

Answers (1)

bdoughan
bdoughan

Reputation: 149047

UPDATE

You should be able to do what you want using a JAXB schema bindings file like the following:

<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="YourSchema.xsd">
        <jxb:bindings node="//xs:element[@name='element']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="byte[]"
                        parseMethod="javax.xml.bind.DatatypeConverter.parseHexBinary" 
                        printMethod="javax.xml.bind.DatatypeConverter.printHexBinary"/>
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

There are a couple of options:

Option #1 - @XmlSchemaType

You can use the @XmlSchemaType annotation to control the schema representation;

@XmlSchemaType(name="hexBinary")
private byte[] hexBinary;

@XmlSchemaType(name="base64Binary")
private byte[] base64Binary;

Option #2 - XmlAdapter

If you want to generate your model from an XML schema and leverage an XmlAdapter instead, the following example demonstrates how it could be done:

Upvotes: 2

Related Questions