JAXB The Java Architecture for XML Binding Notes taken from the JAXB User’s Guide from Sun Microsystems.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
SCJP 6.0 Lecturer Kuo-Yi Chen 151, 153, 154, 155, 156, 157
Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Chapter 1: Computer Systems
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
JAXB Java Architecture for XML Binding Andy Fanton Khalid AlBayat.
XML Parsing Using Java APIs AIP Independence project Fall 2010.
14-Jun-15 DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
Files and Streams CS 21a Chapter 11 of Horstmann.
1 Using JAXB Revisited. 2 An XSDL Document for an ItemList
1 JAXB The Java Architecture for XML Binding Bibliography: - JAXB User’s Guide from Sun Microsystems - Java and XML Data Binding by McLaughlin.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Fonts and colors Times New Roman “quotes” Trebuchet "quotes" yellow blue pink green violet.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
29-Jun-15 JAXB Java Architecture for XML Binding.
JAXB Java Architecture for XML Bindings. What is JAXB? JAXB defines the behavior of a standard set of tools and interfaces that automatically generate.
Internet Technologies JAXB Java API for XML Binding JavaMail.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
CSC3170 Introduction to Database Systems
1 Lecture 22 George Koutsogiannakis Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 3. Implementing Web Services 1.Create SOAP proxy interfaces and WSDL based service descriptions 2.Register/publish services 3.Stores service descriptions.
© 2008 The MITRE Corporation. All rights reserved. Developing an Open Source AIXM5 Java Library (AIXM-J) Steven Chase Lead Software Engineer MITRE/CAASD.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
1 Interfacing & embedding. 2 References  Full tutorial and references Embedding and Interfacing Manual Windows - select “Documentation” from the ECLiPSe.
Overriding toString()
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Introduction to Java John Lewis. Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
Object Oriented Programming Lecture 2: BallWorld.
Data-Driven Programs Eric Roberts CS 106A February 26, 2016.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
CITA 330 Section 2 DTD. Defining XML Dialects “Well-formedness” is the minimal requirement for an XML document; all XML parsers can check it Any useful.
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.
Chapter No. : 1 Introduction to Java.
Software Development Java Classes and Methods
Objects, Classes, Program Constructs
Using Jsoup to Parse HTML
Introduction to OO Program Design
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Accessing Files in Java
Computer Science II Exam 1 Review.
null, true, and false are also reserved.
Introduction to Java Programming
The Boolean (logical) data type boolean
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
Generic Classes and Methods
DOM 24-Feb-19.
Lecture 18: Polymorphism (Part II)
Presentation transcript:

JAXB The Java Architecture for XML Binding Notes taken from the JAXB User’s Guide from Sun Microsystems

JAXB Like SAX and DOM in that the application developer does not have to parse the XML Unlike SAX and DOM it is tied to a particular document schema Fast like SAX In memory representation like DOM but without all the general tree manipulation facilities

JAXB An XML Document is an instance of a schema A Java object is an instance of a class JAXB reads a schema and produces a class Currently, the only schema supported is the Document Type Definition (DTD)

JAXB Augments the standard DTD by providing type conversion : With JAXB we can make sure that Java converts this to a BigDecimal automatically

The Binding Schema JAXB requires that we use a DTD AND a “Binding Schema” The Binding Schema is an XML document JAXB DTD Binding Schema.java file(s) javac.class files javac

The Binding Schema The binding schema contains instructions on how to bind a schema to classes. It might say that : –this attribute (in the XML document) should be treated as an int –this element’s content is a BigDecimal –this element’s content is a String –this element should be treated as a class –and so on …

Running JAXB C:\McCarthy\www\95-733\examples\jaxb>java -jar D:\jaxb\jaxb-1.0-ea\lib\jaxb-xjc-1.0-ea.jar -help Usage: xjc [ ] [ -d ] [ -roots ] Options: -d Specify the destination directory for the Java output -roots Designate one or more root elements (comma separated) -version Print the compiler's version number and exit -help Print this message and exit

Book.xml The Catcher in the Rye J.D. Salinger If you really want to hear about it...

Book.dtd

A minimal binding schema Book.xjc

Run JAXB C:\McCarthy\www\95-733\examples\jaxb>java -jar D:\jaxb\jaxb-1.0-ea\lib\jaxb-xjc-1.0-ea.jar book0.dtd book.xjc.\Book.java C:\McCarthy\www\95-733\examples\jaxb>

Resulting Book.java file import java.io.IOException; import java.io.InputStream; import javax.xml.bind.ConversionException; import javax.xml.bind.Dispatcher; import javax.xml.bind.InvalidAttributeException; import javax.xml.bind.LocalValidationException; import javax.xml.bind.MarshallableRootElement; import javax.xml.bind.Marshaller; import javax.xml.bind.MissingContentException; import javax.xml.bind.RootElement; import javax.xml.bind.StructureValidationException; import javax.xml.bind.UnmarshalException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.Validator; import javax.xml.marshal.XMLScanner; import javax.xml.marshal.XMLWriter;

public class Book extends MarshallableRootElement implements RootElement { private String _Title; private String _Author; private String _Chapter; public String getTitle() { return _Title; } public void setTitle(String _Title) { this._Title = _Title; if (_Title == null) { invalidate(); }

public String getAuthor() { return _Author; } public void setAuthor(String _Author) { this._Author = _Author; if (_Author == null) { invalidate(); } public String getChapter() { return _Chapter; } public void setChapter(String _Chapter) { this._Chapter = _Chapter; if (_Chapter == null) { invalidate(); }

public void validateThis() throws LocalValidationException { if (_Title == null) { throw new MissingContentException("title"); } if (_Author == null) { throw new MissingContentException("author"); } if (_Chapter == null) { throw new MissingContentException("chapter"); } public void validate(Validator v) throws StructureValidationException { }

public void marshal(Marshaller m) throws IOException { XMLWriter w = m.writer(); w.start("book"); w.leaf("title", _Title.toString()); w.leaf("author", _Author.toString()); w.leaf("chapter", _Chapter.toString()); w.end("book"); } public void unmarshal(Unmarshaller u) throws UnmarshalException { XMLScanner xs = u.scanner(); Validator v = u.validator(); xs.takeStart("book"); while (xs.atAttribute()) { String an = xs.takeAttributeName(); throw new InvalidAttributeException(an); }

if (xs.atStart("title")) { xs.takeStart("title"); String s; if (xs.atChars(XMLScanner.WS_COLLAPSE)) { s = xs.takeChars(XMLScanner.WS_COLLAPSE); } else { s = ""; } try { _Title = String.valueOf(s); } catch (Exception x) { throw new ConversionException("title", x); } xs.takeEnd("title"); } if (xs.atStart("author")) { xs.takeStart("author"); String s; if (xs.atChars(XMLScanner.WS_COLLAPSE)) { s = xs.takeChars(XMLScanner.WS_COLLAPSE); } else { s = ""; }

try { _Author = String.valueOf(s); } catch (Exception x) { throw new ConversionException("author", x); } xs.takeEnd("author"); } if (xs.atStart("chapter")) { xs.takeStart("chapter"); String s; if (xs.atChars(XMLScanner.WS_COLLAPSE)) { s = xs.takeChars(XMLScanner.WS_COLLAPSE); } else { s = ""; } try { _Chapter = String.valueOf(s); } catch (Exception x) { throw new ConversionException("chapter", x); } xs.takeEnd("chapter"); }

xs.takeEnd("book"); } public static Book unmarshal(InputStream in) throws UnmarshalException { return unmarshal(XMLScanner.open(in)); } public static Book unmarshal(XMLScanner xs) throws UnmarshalException { return unmarshal(xs, newDispatcher()); } public static Book unmarshal(XMLScanner xs, Dispatcher d) throws UnmarshalException { return ((Book) d.unmarshal(xs, (Book.class))); }

public boolean equals(Object ob) { if (this == ob) { return true; } if (!(ob instanceof Book)) { return false; } Book tob = ((Book) ob); if (_Title!= null) { if (tob._Title == null) { return false; } if (!_Title.equals(tob._Title)) { return false; } } else { if (tob._Title!= null) { return false; }

if (_Author!= null) { if (tob._Author == null) { return false; } if (!_Author.equals(tob._Author)) { return false; } } else { if (tob._Author!= null) { return false; } if (_Chapter!= null) { if (tob._Chapter == null) { return false; } if (!_Chapter.equals(tob._Chapter)) { return false; }

} else { if (tob._Chapter!= null) { return false; } return true; } public int hashCode() { int h = 0; h = ((127 *h)+((_Title!= null)?_Title.hashCode(): 0)); h = ((127 *h)+((_Author!= null)?_Author.hashCode(): 0)); h = ((127 *h)+((_Chapter!= null)?_Chapter.hashCode(): 0)); return h; }

public String toString() { StringBuffer sb = new StringBuffer("<<book"); if (_Title!= null) { sb.append(" title="); sb.append(_Title.toString()); } if (_Author!= null) { sb.append(" author="); sb.append(_Author.toString()); } if (_Chapter!= null) { sb.append(" chapter="); sb.append(_Chapter.toString()); } sb.append(">>"); return sb.toString(); }

public static Dispatcher newDispatcher() { Dispatcher d = new Dispatcher(); d.register("book", (Book.class)); d.freezeElementNameMap(); return d; }

A Driver Program // demo JAXB import java.io.*; public class TestJaxb { public static void main(String args[]) throws java.io.FileNotFoundException, javax.xml.bind.UnmarshalException, java.io.IOException { DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream(args[0]))); DataOutputStream out = new DataOutputStream( new FileOutputStream(args[1]));

// read in the book Book catcher = Book.unmarshal(in); // change its title catcher.setTitle("Gone With The Wind"); // write out the book catcher.marshal(out); }

Compile and Run C:\McCarthy\www\95-733\examples\jaxb>javac Book.java C:\McCarthy\www\95-733\examples\jaxb>javac TestJaxb.java C:\McCarthy\www\95-733\examples\jaxb>java TestJaxb book.xml out.xml

Out.xml Gone With The Wind J.D. Salinger If you really want to hear about it...