JAXB Tips and Tricks Part 2 Generating Java Classes from XML Schema. By Rob Ratcliff



Similar documents
JAXB: Binding between XML Schema and Java Classes

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Java Web Services Training

Java EE 7: Back-End Server Application Development

Copyright 2013 Consona Corporation. All rights reserved

XML Schema Definition Language (XSDL)

Java Interview Questions and Answers

Easy-Cassandra User Guide

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Serialization in Java (Binary and XML)

Java SE 8 Programming

CS 378 Big Data Programming. Lecture 9 Complex Writable Types

Dozer v User's Guide

JVA-561. Developing SOAP Web Services in Java

ITS. Java WebService. ITS Data-Solutions Pvt Ltd BENEFITS OF ATTENDANCE:

Developing Java Web Services

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Java SE 7 Programming

Java SE 7 Programming

Java SE 7 Programming

Concrete uses of XML in software development and data analysis.

Reach 4 million Unity developers

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Modernize your NonStop COBOL Applications with XML Thunder September 29, 2009 Mike Bonham, TIC Software John Russell, Canam Software

TYLER JUNIOR COLLEGE School of Continuing Studies 1530 SSW Loop 323 Tyler, TX

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Java CPD (I) Frans Coenen Department of Computer Science

Java Application Developer Certificate Program Competencies

CS 111 Classes I 1. Software Organization View to this point:

Crash Course in Java

IBM Operational Decision Manager Version 8 Release 5. Getting Started with Business Rules

Connecting to WebSphere ESB and WebSphere Process Server

Android Application Development Course Program

SOA and ESB. Mark Jeynes IBM Software, Asia Pacific

Extending XSLT with Java and C#

Java (12 Weeks) Introduction to Java Programming Language

Contents. 9-1 Copyright (c) N. Afshartous

RMI Client Application Programming Interface

JAVA r VOLUME II-ADVANCED FEATURES. e^i v it;

AP Computer Science Java Subset

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

InfoSphere Master Data Management operational server v11.x OSGi best practices and troubleshooting guide

Java EE Web Development Course Program

CA Aion Rule Manager. Rule Engine: JSR-94 Implementation Guide. r11

Aspect Oriented Programming. with. Spring

Eclipse 4 RCP application Development COURSE OUTLINE

10CS73:Web Programming

Lecture 5: Java Fundamentals III

JAVA API FOR XML WEB SERVICES INTRODUCTION TO JAX-WS, THE JAVA API FOR XML BASED WEB SERVICES (SOAP, WSDL)

EclipseLink. Solutions Guide for EclipseLink Release 2.5

Syllabus for CS 134 Java Programming

Facebook Twitter YouTube Google Plus Website

Oracle Business Rules: Technical Overview. An Oracle White Paper February 2007

ASM 4.0 A Java bytecode engineering library. Eric Bruneton

OUR COURSES 19 November All prices are per person in Swedish Krona. Solid Beans AB Kungsgatan Göteborg Sweden

Developing Web Services with Apache CXF and Axis2

Spring Data JDBC Extensions Reference Documentation

AVRO - SERIALIZATION

Oracle Java CAPS Message Library for EDIFACT User's Guide

Module 13 Implementing Java EE Web Services with JAX-WS

Corticon Studio: Extensions Guide

JAVA IN A NUTSHELL O'REILLY. David Flanagan. Fifth Edition. Beijing Cambridge Farnham Köln Sebastopol Tokyo

JAVA API FOR XML WEB SERVICES (JAX-WS)

An Overview of Java. overview-1

Developer Guide. Christian W. Günther Library version: 1.0 RC7 Revision: 7

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Lesson 4 Web Service Interface Definition (Part I)

TIBCO ActiveMatrix BusinessWorks Error Codes. Software Release 5.7 December 2008

Spring 3.1 to 3.2 in a Nutshell. Sam Brannen Senior Software Consultant

SDK Quick Start Guide

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Drupal CMS for marketing sites

WEB SERVICES. Revised 9/29/2015

Introduction to Eclipse

Java Card Development Kit. 1 Table of Contents. 2 Introduction. Release Notes for Java Card 3 Platform, Classic Edition, Version 3.0.

Pemrograman Dasar. Basic Elements Of Java

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Realizing Enterprise Integration Patterns in WebSphere

Integration and Configuration of SofwareAG s webmethods Broker with JBOSS EAP 6.1

Java Programming Language

NetBeans IDE Field Guide

Habanero Extreme Scale Software Research Project

Schema Classes. Polyhedra Ltd

COSC 6397 Big Data Analytics. 2 nd homework assignment Pig and Hive. Edgar Gabriel Spring 2015

Module Title: : Cloud Application Development

WIRIS quizzes web services Getting started with PHP and Java

Web Services Development In a Java Environment

Software documentation systems

Introduction to Synoptic

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

CS 106 Introduction to Computer Science I

Introduction to Java. CS 3: Computer Programming in Java

Spring,2015. Apache Hive BY NATIA MAMAIASHVILI, LASHA AMASHUKELI & ALEKO CHAKHVASHVILI SUPERVAIZOR: PROF. NODAR MOMTSELIDZE

OpenOffice.org Extensions development in Java with NetBeans in practise. Jürgen Schmidt OpenOffice.org Sun Microsystems, Inc.

DIPLOMADO DE JAVA - OCA

Transcription:

JAXB Tips and Tricks Part 2 Generating Java Classes from XML Schema By Rob Ratcliff

What is JAXB? Java Architecture for XML Binding Maps an XML Schema into Java Objects Experimental support for DTD, RelaxNG and WSDL Generates classes by default, but can specify existing target classes Generates a XML Schema from annotated Java Objects Saves programmers labor and debug time compared to manual parsing, especially as schemas evolve

History Version 1.0 released in 2003 as part of JSR 31 Marshalling and Unmarsalling code generated Version 2.0 released in 2006 as part of JSR 222 for Java 6 Reflection based marshalling and unmarshalling code much cleaner Version 2.2 released in 2009 Version 2.2.3 bundled with Java 7 Version 2.2.8 bundled with Java 8 Support for 3 rd party implementations of JAXB such as EclipseLink/MOXy jaxb-impl.jar has to be on classpath not in endorsed directory or endorsed classpath jaxb-api.jar in endorsed directory or endorsed classpath Latest release is 2.2.11 JAXB has a git repository

Top 10 Reasons JAXB is a Fav! 1. Built into the JDK, but overridable with new versions or 3 rd party implementations 2. Easy to use 3. Well documented 4. Logical API 5. Robust and Efficient implementation 6. Complete control of the fields that get serialized to XML 7. Handles mapping from XML Schema or Annotated Java classes 8. Community generated plugins to customize code generation 9. Good default mappings 10. Default mappings can be completely overridden

XML Schema to Java Classes

Default XML Schema to Java Bindings XML Schema Data Type Java Data Type (lower case indicates a primitive data type) anysimpletype (for xsd:element of this type) anysimpletype (for xsd:attribute of this type) base64binary boolean byte date datetime decimal double duration float g hexbinary int integer long NOTATION Qname short string time unsignedbyte unsignedint unsignedshort java.lang.object java.lang.string byte[] boolean or (Boolean) byte (or Byte) java.xml.datatype.xmlgregoriancalendar javax.xml.datatype.xmlgregoriancalendar java.math.bigdecimal double (or Double) javax.xml.datatype.duration float (or Float) java.xml.datatype.xmlgregoriancalendar byte[] int (or Integer) java.math.biginteger long or (Long) javax.xml.namespace.qname javax.xml.namespace.qname short (or Short) java.lang.string java.xml.datatype.xmlgregoriancalendar short (or Short) long (or Long) int

Generating Java Classes from Schema xjc -extension geometry.xsd -b geometry.xjb

Converting Java Classes from Schema xjc geometry.xsd -b geometry.xjb Binding File <globalbindings> [ collectiontype = "collectiontype" ] [ fixedattributeasconstantproperty = "true" "false" "1" "0" ] [ generateissetmethod = "true" "false" "1" "0" ] [ enablefailfastcheck = "true" "false" "1" "0" ] [ choicecontentproperty = "true" "false" "1" "0" ] [ underscorebinding = "aswordseparator" "ascharinword" ] [ typesafeenumbase = "typesafeenumbase" ] [ typesafeenummembername = "generatename" "generateerror" ] [ enablejavanamingconventions = "true" "false" "1" "0" ] [ bindingstyle = "elementbinding" "modelgroupbinding" ] [ <javatype>... </javatype> ]* </globalbindings>

JAXB Binding File <jaxb:bindings version="2.0" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionbindingprefixes="xjc"> <jaxb:bindings schemalocation= geometry.xsd"> <jaxb:globalbindings> <jaxb:serializable/> <xjc:javatype name="java.sql.timestamp" xmltype="xs:datetime" adapter="timeconverter"/> </jaxb:globalbindings> <jaxb:schemabindings> </jaxb:schemabindings> </jaxb:bindings> </jaxb:bindings>

Custom Mapping Color Polygons DateTime Wrapper Elements

Some Useful JAXB Plugins for Customizing Generated Java Classes from XML Schemas Fluent API Plugin Generate fluent design pattern for setters i.e. returns this for each setter so that sets can be chained as in: person.setfirstname(firstname).setlastname(lastname).setaddress(address); JAXB2 Basics Plugin generates tostring(), equals(), hashcode(), etc. Value Constructor generates a constructor with arguments HyperJAXB3 Generates JPA annotated classes http://confluence.highsource.org/display/hj3/home See https://java.net/projects/jaxb2-commons/pages/home for more plugins JAK Java API for KML - https://github.com/micromata/javaapiforkml

Converting XML to Java StringReader reader = new StringReader(xmlString); JAXBContext context = JAXBContext.newInstance(ObjectFactory.class); ShapeList shape = (ShapeList) context.createunmarshaller().unmarshal(reader);

nillable vs. minoccurs=0? Variants <myelement attr1='true'>some content</myelement> <myelement/> <myelement xsi:nil='true'/> Missing <myelement/> minoccurs=1 will map to the Java primitive minoccurs=0 or nillable=true will map to the Object version of primitives minoccurs=0 and nillable=true will map to JAXBElement<Double>

Validating Against a Schema No validation by default JAXB tries to be forgiving when there is an extra field or missing field to help deal with version Just Unmarshaller.setValidating(true) to begin validating

Converting Java to XML JAXBContext context = JAXBContext.newInstance(ObjectFactory.class); StringWriter stringwriter = new StringWriter(); Marshaller marshaller = context.createmarshaller(); marshaller.setproperty("jaxb.formatted.output", Boolean.TRUE); marshaller.marshal(jaxbelement, stringwriter); return stringwriter.tostring();

Tips 1. Performance 1. Cache the Threadsafe JAXBContext for much better performance put in a static or singleton 2. Marshallers and Unmarshallers are not Threadsafe so if performance is critical use pools of these 2. Don t embed JAXB customizations in schema, put in a binding file 3. Good mapping requires iterations on schema, binding customizations and the resulting Java code

Schema Evolution/Versioning Schema Versioning Approaches Add version as part of the namespace (don t use a raw number) Add a version field in the XML Java package name generated from target namespace by default Advantage: Allows multiple versions of the schema to be handled in the same classloader Disadvantage: Package names of consuming code has to be changed for new versions of the schema Use a different classloader (e.g. Session Bean) for each version accessed through a common Java interface Advantage: Do not need to change consuming code Disadvantage: Added complexity

XML Schema Editors http://deadlock.netbeans.org/hudson/job/xml/lastsuccessfulbuild/artifact/build/updates/upd ates.xml http://wiki.eclipse.org/index.php/introduction_to_the_xsd_editor

Current Deficits 1. Annotations/documentation doesn t get translated to JavaDoc 2. Restrictions don t get translated into validation code (MOXy may do this)

Questions?

Graphs using ID and IDREF