The Extensible Markup Language (XML) and Java technology are natural partners in helping developers exchange data and programs across the Internet.

Similar documents
Java EE 7: Back-end Server Application Development 4-2

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

Using the JAXB Wizard and Code-Seeder Pallete

Introduction to XML. XML: basic elements

XML Parsers. Asst. Prof. Dr. Kanda Runapongsa Saikaew Dept. of Computer Engineering Khon Kaen University

.. Cal Poly CPE/CSC 366: Database Modeling, Design and Implementation Alexander Dekhtyar..

Chapter 13 XML: Extensible Markup Language

DEVELOPING A MESSAGE PARSER TO BUILD THE TEST CASE GENERATOR

XML. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

XML: Extensible Markup Language

M359 Block5 - Lecture12 Eng/ Waleed Omar

Jaxb Validate Xml Against Multiple Xsd

XML for Java Developers G Session 3 - Main Theme XML Information Modeling (Part I) Dr. Jean-Claude Franchitti

XML for Java Developers G Session 3 - Main Theme XML Information Modeling (Part I) Dr. Jean-Claude Franchitti

Chapter 1: Getting Started. You will learn:

Supporting Class / C++ Lecture Notes

Workshop for WebLogic introduces new tools in support of Java EE 5.0 standards. The support for Java EE5 includes the following technologies:

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 27-1

Oracle Enterprise Pack for Eclipse 11g Hands on Labs

Java Web Service Essentials (TT7300) Day(s): 3. Course Code: GK4232. Overview

CSI 3140 WWW Structures, Techniques and Standards. Representing Web Data: XML

11. EXTENSIBLE MARKUP LANGUAGE (XML)

Call: Core&Advanced Java Springframeworks Course Content:35-40hours Course Outline

Java J Course Outline

Agenda. Summary of Previous Session. XML for Java Developers G Session 6 - Main Theme XML Information Processing (Part II)

XML: Introduction. !important Declaration... 9:11 #FIXED... 7:5 #IMPLIED... 7:5 #REQUIRED... Directive... 9:11

Smooks Developer Tools Reference Guide. Version: GA

Exam : Title : Sun Certified Developer for Java Web Services. Version : DEMO

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 7 XML

SUN. Java Platform Enterprise Edition 6 Web Services Developer Certified Professional

The Xlint Project * 1 Motivation. 2 XML Parsing Techniques

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

Data Exchange. Hyper-Text Markup Language. Contents: HTML Sample. HTML Motivation. Cascading Style Sheets (CSS) Problems w/html

Lesson 10 BPEL Introduction

Xsd Schema Validation Warning Type Is Not. Declared >>>CLICK HERE<<<

Jdom Interest Schema Validation Example

XML Processor User Guide

Programming Web Services in Java

XML: Tools and Extensions

Data Presentation and Markup Languages

Java Import Xsd Schema Validate Xml Against Multiple

1Z Oracle. Java Platform Enterprise Edition 6 Web Services Developer Certified Expert

Intro to XML. Borrowed, with author s permission, from:

XML: Tools and Extensions

Introduction p. 1 An XML Primer p. 5 History of XML p. 6 Benefits of XML p. 11 Components of XML p. 12 BNF Grammar p. 14 Prolog p. 15 Elements p.

Introducing EclipseLink: The Eclipse Persistence Services Project

2009 Martin v. Löwis. Data-centric XML. XML Syntax

Building Web Sites Using the EPiServer Content Framework

7.1 Introduction. extensible Markup Language Developed from SGML A meta-markup language Deficiencies of HTML and SGML

extensible Markup Language

Managing Application Configuration Data with CIM

XML for Java Developers G Session 8 - Main Theme XML Information Rendering (Part II) Dr. Jean-Claude Franchitti

IT6801-SERVICE ORIENTED ARCHITECTURE

XML Processing & Web Services. Husni Husni.trunojoyo.ac.id

1. Description. 2. Systems affected Wink deployments

1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

XML Web Services Basics

Agenda. Summary of Previous Session. XML for Java Developers G Session 7 - Main Theme XML Information Rendering (Part II)

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

TagSoup: A SAX parser in Java for nasty, ugly HTML. John Cowan

Delivery Options: Attend face-to-face in the classroom or remote-live attendance.

I Got My Mojo Workin'

STARCOUNTER. Technical Overview

COMP9321 Web Application Engineering

Create web pages in HTML with a text editor, following the rules of XHTML syntax and using appropriate HTML tags Create a web page that includes

XML ELECTRONIC SIGNATURES

Schema For Namespace Already Contains Type

Programming Languages and Translators COMS W4115. Department of Computer Science. Fall TweaXML. Language Proposal

Delivery Options: Attend face-to-face in the classroom or via remote-live attendance.

Rich Web Application Backplane

Chapter 8 Web Services Objectives

Oracle Application Server 10g Oracle XML Developer s Kit Frequently Asked Questions September, 2005

Stream. Two types of streams are provided by Java Byte and Character. Predefined Streams

jquery Tutorial for Beginners: Nothing But the Goods

CaptainCasa Enterprise Client. CaptainCasa Enterprise Client. CaptainCasa & Java Server Faces

Acceleration Techniques for XML Processors

Markup Languages SGML, HTML, XML, XHTML. CS 431 February 13, 2006 Carl Lagoze Cornell University

Operating Systems. 18. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Spring /20/ Paul Krzyzanowski

Introduction to XML. Asst. Prof. Dr. Kanda Runapongsa Saikaew Dept. of Computer Engineering Khon Kaen University

What is XML? XML is designed to transport and store data.

Aegis (2.1) What is Aegis? Getting Started: Basic Use of Aegis. Aegis Operations - The Simple Case

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

Service Interface Design RSVZ / INASTI 12 July 2006

XML: Managing with the Java Platform

Freedom of Choice: Using Object Code Completion in the IDE

Speech 2 Part 2 Transcript: The role of DB2 in Web 2.0 and in the IOD World

Part V. SAX Simple API for XML

Part V. SAX Simple API for XML. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 84

EXtensible Markup Language XML

XML. extensible Markup Language. Overview. Overview. Overview XML Components Document Type Definition (DTD) Attributes and Tags An XML schema

Elliotte Rusty Harold August From XML to Flat Buffers: Markup in the Twenty-teens

Chapter 2 XML, XML Schema, XSLT, and XPath

Introduction to XML 3/14/12. Introduction to XML

1 Getting used to Python

XML Querying and Communication Classes for the VizIR Framework

A tutorial report for SENG Agent Based Software Engineering. Course Instructor: Dr. Behrouz H. Far. XML Tutorial.

StASOAP: Streaming API for SOAP

Distributed Systems 8. Remote Procedure Calls


A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Transcription:

1

2

3

The Extensible Markup Language (XML) and Java technology are natural partners in helping developers exchange data and programs across the Internet. That's because XML has emerged as the standard for exchanging data across disparate systems, and Java technology provides a platform for building portable applications. This partnership is particularly important for Web services, which promise users and application developers program functionality on demand from anywhere to anywhere on the Web. XML and Java technology are recognized as ideal building blocks for developing Web services and applications that access Web services. 4

But how do you couple these partners in practice? More specifically, how do you access and use an XML document (that is, a file containing XML-tagged data) through the Java programming language? One way to do this, perhaps the most typical way, is through parsers that conform to the Simple API for XML (SAX) or the Document Object Model (DOM). Both of these parsers are provided by Java API for XML Processing (JAXP). Java developers can invoke a SAX or DOM parser in an application through the JAXP API to parse an XML document -- that is, scan the document and logically break it up into discrete pieces. The parsed content is then made available to the application. In the SAX approach, the parser starts at the beginning of the document and passes each piece of the document to the application in the sequence it finds it. Nothing is saved in memory. The application can take action on the data as it gets it from the parser, but it can't do any in-memory manipulation of the data. For example, it can't update the data in memory and return the updated data to the XML file. In the DOM approach, the parser creates a tree of objects that represents the content and organization of data in the document. In this case, the tree exists in memory. The application can then navigate through the tree to access the data it needs, and if appropriate, manipulate it. 5

6

Suppose you need to develop a Java application that accesses and displays data in XML documents such as books.xml. These documents contain data about books, such as book name, author, description, and ISBN identification number. You could use the SAX or DOM approach to access an XML document and then display the data. For example, suppose you took the SAX approach. In that case, you would need to: Write a program that creates a SAX parser and then uses that parser to parse the XML document. The SAX parser starts at the beginning of the document. When it encounters something significant (in SAX terms, an "event") such as the start of an XML tag, or the text inside of a tag, it makes that data available to the calling application. Create a content handler that defines the methods to be notified by the parser when it encounters an event. These methods, known as callback methods, take the appropriate action on the data they receive. 7

Now let's look at how you use JAXB to access an XML document such as books.xml and display its data. Using JAXB, you would: Bind the schema for the XML document. Unmarshal the document into Java content objects. The Java content objects represent the content and organization of the XML document, and are directly available to your program. After unmarshalling, your program can access and display the data in the XML document simply by accessing the data in the Java content objects and then displaying it. There is no need to create and use a parser and no need to write a content handler with callback methods. What this means is that developers can access and process XML data without having to know XML or XML processing. 8

JAXB simplifies access to an XML document from a Java program by presenting the XML document to the program in a Java format. The first step in this process is to bind the schema for the XML document into a set of Java classes that represents the schema. Schema: A schema is an XML specification that governs the allowable components of an XML document and the relationships between the components. For example, a schema identifies the elements that can appear in an XML document, in what order they must appear, what attributes they can have, and which elements are subordinate (that is, are child elements) to other elements. Assume, for this example, that the books.xml document has a schema, books.xsd, that is written in the W3C XML Schema Language. This schema defines a <Collection> as an element that has a complex type. This means that it has child elements, in this case, <book> elements. Each <book> element also has a complex type named booktype. The <book> element has child elements such as <name>, <ISBN>, and <author>. Some of these have their own child elements. 9

For example, the JAXB Reference Implementation provides a binding compiler that you can invoke through scripts. 10

The -p option identifies a package for the generated classes, and the -d option identifies a target directory. So for this command, the classes are packaged in test.jaxb within the work directory. In response, the binding compiler generates a set of interfaces and a set of classes that implement the interfaces. Here are the interfaces it generates for the books.xsd schema: 11

Because the classes are implementation-specific, classes generated by the binding compiler in one JAXB implementation will probably not work with another JAXB implementation. So if you change to another JAXB implementation, you should rebind the schema with the binding compiler provided by that implementation. In total, the generated classes represent the entire books.xsd schema. Notice that the classes define get and setmethods that are used to respectively obtain and specify data for each type of element and attribute in the schema. You then compile the generated interfaces and classes. 12

Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document. The content tree is not a DOM-based tree. In fact, content trees produced through JAXB can be more efficient in terms of memory use than DOM-based trees. The content objects are instances of the classes produced by the binding compiler. In addition to providing a binding compiler, a JAXB implementation must provide runtime APIs for JAXB-related operations such as marshalling. The APIs are provided as part of a binding framework. 13

This object provides the entry point to the JAXB API. When you create the object, you need to specify a context path. This is a list of one or more package names that contain interfaces generated by the binding compiler. By allowing multiple package names in the context path, JAXB allows you to unmarshal a combination of XML data elements that correspond to different schemas. This object controls the process of unmarshalling. In particular, it contains methods that perform the actual unmarshalling operation This method does the actual unmarshalling of the XML document. Recall that the classes that a JAXB compiler generates for a schema include get and set methods you can use to respectively obtain and specify data for each type of element and attribute in the schema. You can validate source data against an associated schema as part of the unmarshalling operation. 14

15

Instead of accessing data in an XML document, suppose you need to build an XML document through a Java application. Here too using JAXB is easier. Let's investigate. This is the same operation you perform prior to unmarshalling a document. In this case, the schema is for the XML document you want to build. Of course, if you've already bound the schema (for instance, you unmarshalled an XML document, updated the data, and now want to write the updated data back to the XML document), you don't have to bind the schema again. Create the Content Tree The content tree represents the content that you want to build into the XML document. You can create the content tree by unmarshalling XML data, or you can create it using the ObjectFactory class that's generated by binding the appropriate schema. 16

Marshalling is the opposite of unmarshalling. It creates an XML document from a content tree. To marshal a content tree, you: Create a JAXBContext object, and specify the appropriate context path -- that is, the package that contains the classes and interfaces for the bound schema. Create a Marshaller object. This object controls the process of marshalling. In particular, it contains methods that perform the actual marshalling operation. The Marshaller object has properties that you can set through the setproperty method. For example, you can specify the output encoding to be used when marshalling the XML data. Or you can tell the Marshaller to format the resulting XML data with line breaks and indentation. Call the marshal method. This method does the actual marshalling of the content tree. When you call the method, you specify an object that contains the root of the content tree, and the output target. Validator validator = jaxbcontext.createvalidator(); validator.validate(collection); 17

18

Here, by comparison, is a JAXB program that updates an XML document. Specifically, it updates an unmarshalled content tree and then marshals it back to an XML document. Notice how JAXB simplifies the process. The program has direct access to the object it needs to update. The program uses a get method to access the data it needs, and a set method to update the data. Although it's tempting to think that the XML data can make a "roundtrip" unchanged, there's no guarantee of that. In other words, if you use JAXB to unmarshal an XML document and then marshal it back to the same XML file, there's no guarantee that the XML document will look exactly the same at it did originally. For example, the indentation of the resulting XML document might be a bit different than the original. The JAXB specification does not require the preservation of the XML information set in a roundtrip from XML document-to-java representation-to XML document. But it also doesn't forbid the preserving of it. 19

20

For example, the XML built-in datatype xsd:string must be bound to the Java data type java.lang.string. For example, suppose you want an XML data type mapped to a Java data type that is different than the type called for by the default binding specification. Or you want the binding compiler to assign a name of your choice to a class that it generates. 21

22

All binding declarations are in an annotation element and its subordinate appinfo element. In fact, all inline binding declarations must be made this way. Make global customizations: The <jaxb:globalbindings...> element specifies binding declarations that have global scope. In JAXB, binding declarations can be specified at different levels, or "scopes." Each scope inherits from the scopes above it, and binding declarations in a scope override binding declarations in scopes above it. Add method signatures. The declaration generateissetmethod="true" tells the binding compiler to generate isset methods for the properties of all generated classes. Change binding style. By default, schema components that have complex types and that have a content type property of mixed or element-only are bound with a style called element binding. In element binding, each element in the complex type is mapped to a unique content property. Alternatively, you can change the binding style to model group binding by specifying bindingstyle="modelgroupbinding" choicecontentproperty="true". In model group binding, schema components that have complex type and that are nested in the schema are mapped to Java interfaces. This gives users a way to specifically customize these nested components. 23

24

25

26