JAXB Java API for XML Binding. The Objective JAXB is concerned with the translation process between Java objects and XML documents You have a Java object.

Slides:



Advertisements
Similar presentations
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Web Services with Apache CXF
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
Open-DIS Open Source Distributed Interactive Simulation Don McGregor (mcgredo at nps dot edu) Don Brutzman (brutzman at nps dot edu) John Grant (johnkonradgrant.
Open-DIS and XML DIS in Other Formats. Distributed Interactive Simulation DIS is an IEEE standard for simulations, primarily virtual worlds Binary protocol:
JAXB Java Architecture for XML Binding Andy Fanton Khalid AlBayat.
Web Services with Apache CXF
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Fonts and colors Times New Roman “quotes” Trebuchet "quotes" yellow blue pink green violet.
29-Jun-15 JAXB Java Architecture for XML Binding.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
JAXB Java Architecture for XML Bindings. What is JAXB? JAXB defines the behavior of a standard set of tools and interfaces that automatically generate.
Introduction to XML Chien-Chung Shen CIS, UD
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
AIXM Viewer Implementation Presentation to AIXM Users’ Conference, Federal Aviation Administration, Washington D.C., Feb 27-March 1, By: Mark Austin,
XP New Perspectives on XML Tutorial 4 1 XML Schema Tutorial – Carey ISBN Working with Namespaces and Schemas.
GT3 Tutorial Chapter 5, 6, and 7 Lecture for Cluster and Grid Computing, CSCE 490/590 Fall 2004, University of Arkansas, Dr. Amy Apon
Inheritance using Java
1 Lecture 22 George Koutsogiannakis Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Eclipse – making OOP Easy
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Object-oriented tools for XML Schema JAXB (Java Architecture for XML Binding) –object connectivity for XML documents HyperJAXB –relational persistence.
Lecture 6 of Advanced Databases XML Schema, Querying & Transformation Instructor: Mr.Ahmed Al Astal.
Neminath Simmachandran
Introduction to XML. XML - Connectivity is Key Need for customized page layout – e.g. filter to display only recent data Downloadable product comparisons.
Sheet 1XML Technology in E-Commerce 2001Lecture 7 XML Technology in E-Commerce Lecture 7 XSL Formatting Objects, Java Data Binding.
JBoss at Work JAW Motors Application Chapter 1-3 Jeff Schmitt October 9, 2006.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
© 2006 by BEA Systems Inc; made available under the EPL v1.0 | March 2006 | Java Annotation Processing (APT) in the Eclipse JDT Gary Horen BEA Systems.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
CSC 212 Object-Oriented Programming and Java Part 1.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
Or, Hey can’t we just do it using HTTP for the envelope?
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
Web Architecture: Extensible Language Tim Berners-Lee, Dan Connolly World Wide Web Consortium 元智資工所 系統實驗室 楊錫謦 1999/9/15.
Ant Presentation by: Bart Taylor. What is Ant? The simple definition: A Java-based build tool The Official Definition: “Apache Ant is a Java-based build.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Sheet 1 DocEng’03, Grenoble, November 2003 Model Driven Architecture based XML Processing Ivan Kurtev, Klaas van den Berg University of Twente, the Netherlands.
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
CS 157B: Database Management Systems II February 11 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
CS 157B: Database Management Systems II February 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
SimDB Implementation & Browser IVOA InterOp 2008 Meeting, Theory Session 1. Baltimore, 26/10/2008 Laurent Bourgès This work makes use of EURO-VO software,
Atos, Atos and fish symbol, Atos Origin and fish symbol, Atos Consulting, and the fish itself are registered trademarks of Atos Origin SA. August 2006.
问题 Code scattering Blocks of duplicated code Blocks of complementary code, and different modules implementing complementary parts of the concern Code.
Java Projections in SDO 3.0 First Draft Ron Barack.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Lecture 23 XQuery 1.0 and XPath 2.0 Data Model. 2 Example 31.7 – User-Defined Function Function to return staff at a given branch. DEFINE FUNCTION staffAtBranch($bNo)
Experience with XML Schema Ashok Malhotra Schema Usage  Mapping XML Schema and XML documents controlled by the Schema to object classes and instances.
JAXB (Java Architecture for XML Binding) Internet Computing KUT ( Youn-Hee Han.
1 Entity Callbacks and Listeners When you execute EntityManager methods like persist( ), merge( ), remove( ), and find( ), or when you execute EJB QL queries,
4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema.
Java Standard Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 4 Final.
Service Oriented Architecture.  SOA is an architectural pattern in software design.  SOA application components provide services to other components.
The Java API for XML Binding. The Need for Data Binding To support unmarshlling to convert XML data into Java objects. To support marshalling to convert.
Web Services-JAX-RPC JAX-RPC enables a Web Service endpoint to be developed using either a Java Servlet or Enterprise JavaBeans (EJB) component model.
Java Beans Sagun Dhakhwa.
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Chapter 9 Web Services: JAX-RPC, WSDL, XML Schema, and SOAP
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Java Programming Language
CSG2H3 Object Oriented Programming
Presentation transcript:

JAXB Java API for XML Binding

The Objective JAXB is concerned with the translation process between Java objects and XML documents You have a Java object and want to write it to XML You have an XML document and want to turn it into Java object(s) Demo code at emos

Tasks There are several possible starting points for working with Java and XML: Write a Java class that can be marshaled to XML Given a Java class in “java bean” format, generate an XML schema Given an XML schema, generate Java classes that can read and write XML This intro assumes JDK 1.6 (which incorporates Jaxb 2.x)

Annotations Annotations are a new feature of JDK 1.5 and later. Essentially they are markers in the Java source code that can be used by external tools to generate code Format looks Annotations can occur only in specific places in the code, including before a class definition, before a method declaration, before an ivar, etc. /** annotations can appear before a method Public float getX() { return x;}

Java to XML POJO (Plain Old Java Object) to XML. Assume that you want to make a Java class writeable to XML. Suppose you have this: public class Point { float x; float y; } With a getter and setter for each in javabeans format, eg public void setX(float x) public float getX()

Annotations Markup See source code. Things to to designate a field as an attribute rather than an element. The field must be a primitive schema type (float, int, to designate the document root element. You can also specify a to specify the element that encloses a repeating series of elements Note that you should specify only the getter method Jaxb oddly treats both the field and the getter method as independent entities to annotate things that should not be marshalled to XML.

Generate an XML Schema from Java Code Suppose you already have Java code (annotated or not) and want to generate an XML schema. How do you do this? JDK 1.6 includes schemagen in the jdk/bin directory, which can accomplish this. Typically you do this with an Ant task, but it’s simpler to show this from the command line

Schemagen Note that schemagen can operate on any javabeans compliant class, not just annotated ones. if it has in ivar called “foo”, it must have getFoo() and setFoo() No-args constructor

Generate XML Schema from Java C:\Program Files\java\jdk1.6.0_03\bin\schemagen -cp src\edu\nps\moves\demo src\edu\nps\moves\demo*.java Places the schema into schema1.xsd

Fragment from Schema

What Happened? This illustrates one of the possible starting points: you have Java classes, and want a corresponding XML schema. We just generated that schema

Generate Java from Schema This is another possible starting point: you have an XML schema, and want to create Java classes that correspond to that JAXB can generate simple java beans classes with getters and setters that know how to unmarshall themselves from XML This simplifies the parsing process for small-to- medium XML documents: use JAXB to read it into Java classes

xjc The program that does this is xjc, also included in the bin director of the JDK The command: "c:\Program Files\Java\jdk1.6.0_03\bin\xjc" -d src\jaxb -p edu.nps.moves.jaxb schema1.xsd -p specifies the package the generated java will use, -d the directory to which the classes should be written, schema1.xsd the schema to use when creating the classes

Fragment of generated code public class Point = true) protected float = true) protected float = true) protected float z;

ObjectFactory You generally create new instances of the generated classes via ObjectFactory See Usage.java class for examples NOTE: for obscure reasons, JAXB does not insert annotation where it “should”. The easiest way to handle this is to just add the annotation manually to the Points class, the root element of the document. There are other workarounds, but they appear to be experimental right now

Marshalling See Usage.java for full examples of marshalling and unmarshalling to XML JAXBContext context = JAXBContext.newInstance(Points.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(pointContainer, new FileOutputStream("example.xml"));

Unmarshalling Unmarshaller unmarshaller = context.createUnmarshaller(); Points unmarshalledObject = (Points)unmarshaller.unmarshal(new FileInputStream("example.xml"));

What Happened? Annotate a POJO Java class so we can marshal & unmarshall it to XML Given a Java class, generate an XML schema Given an XML schema, generate Java classes