DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Schema to Java: @XmlMimeType & @XmlInlineBinaryData

Schema to Java: @XmlMimeType & @XmlInlineBinaryData

Blaise Doughan user avatar by
Blaise Doughan
·
Jan. 03, 12 · Interview
Like (0)
Save
Tweet
Share
4.78K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous post I described the impact of the @XmlInlineBinaryData and @XmlMimeType annotations in a JAX-WS environment where binary data may be sent as attachments.  One of the comments I received on that post was how to have those annotations generated onto your classes when starting from an XML schema.  In this post I will address that question.


Java Model

Below is the type of Java model we are trying to generate.  The accessors have been omitted to save space.

	
package com.example;
 
import java.awt.Image;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlInlineBinaryData;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"a","b","c","d"})
@XmlRootElement(name = "root")
public class Root {
 
    protected byte[] a;
 
    protected byte[] b;
 
    @XmlInlineBinaryData
    protected byte[] c;
 
    @XmlMimeType("image/jpeg")
    protected Image d;
 
}

 


XML Schema

Below is the XML schema we will be starting with.
<?xml version="1.0"?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.example.com"
    targetNamespace="http://www.example.com">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="a"
                    minOccurs="0"
                    type="xsd:base64Binary"/>
                <xsd:element name="b"
                    minOccurs="0"
                    type="xsd:base64Binary"/>
                <xsd:element name="c"
                    minOccurs="0"
                    type="xsd:base64Binary"/>
                <xsd:element name="d"
                    minOccurs="0"
                    type="xsd:base64Binary"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

If we generate Java classes from the above schema the resulting class will look like (accessors have been omitted to save space):
package com.example;
 
import java.awt.Image;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlInlineBinaryData;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"a","b","c","d"})
@XmlRootElement(name = "root")
public class Root {
 
    protected byte[] a;
 
    protected byte[] b;
 
    protected byte[] c;
 
    protected byte[] d;
 
}

@XmlMimeType and Image

To generate our d field to be of type Image annotated with @XmlMimeType we specify the xmime:expectedContentTypes attribute on the d element.
<?xml version="1.0"?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
    xmlns="http://www.example.com"
    targetNamespace="http://www.example.com">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="a"
                    minOccurs="0"
                    type="xsd:base64Binary"/>
                <xsd:element name="b"
                    minOccurs="0"
                    type="xsd:base64Binary"/>
                <xsd:element name="c"
                    minOccurs="0"
                    type="xsd:base64Binary"/>
                <xsd:element name="d"
                    minOccurs="0"
                    type="xsd:base64Binary"
                    xmime:expectedContentTypes="image/jpeg"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Now when we generate our class it will look like:
package com.example;
 
import java.awt.Image;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlInlineBinaryData;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"a","b","c","d"})
@XmlRootElement(name = "root")
public class Root {
 
    protected byte[] a;
 
    protected byte[] b;
 
    protected byte[] c;
 
    @XmlMimeType("image/jpeg")
    protected Image d;
 
}

@XmlInlineBinaryData

To generate the @XmlInlineBinaryData annotation we need to customize our XML schema.  This can either be done inline, or as below with a JAXB bindings file:

Bindings File (bindings.xml)
<jxb:bindings
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
 
    <jxb:bindings schemaLocation="binary.xsd">
        <jxb:bindings node="//xsd:element[@name='root']/xsd:complexType/xsd:sequence/xsd:element[@name='c']">
            <jxb:inlineBinaryData/>
        </jxb:bindings>
    </jxb:bindings>
 
</jxb:bindings>

The bindings file is passed as a parameter to the XJC call:
xjc -d out -b bindings.xml binary.xsd

The resulting Java class will be:
package com.example;
 
import java.awt.Image;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlInlineBinaryData;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"a","b","c","d"})
@XmlRootElement(name = "root")
public class Root {
 
    protected byte[] a;
 
    protected byte[] b;
 
    @XmlInlineBinaryData
    protected byte[] c;
 
    @XmlMimeType("image/jpeg")
    protected Image d;
 
}

 

From http://blog.bdoughan.com/2011/05/schema-to-java-xmlmimetype.html

Schema Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Enterprise Portal and How to Develop One?
  • What Is Docker Swarm?
  • Leverage Lambdas for Cleaner Code
  • gRPC on the Client Side

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: