|By Tilak Mitra||
|March 30, 2004 12:00 AM EST||
JAXB (Java Architecture for XML Binding) provides a convenient way to bind an XML Schema to a representation in Java code and makes it easy for developers to incorporate XML data and processing functions into applications based on Java technology without having to know much about XML itself.
It has been proven over the past few years that the best form of information exchange (in a typical B2B or B2C scenario) is through XML. There are various XML-based standards (schemas) for both the horizontal and vertical market sectors, and there are ongoing efforts to move toward a standardized format.
With the proliferation of XML-based information exchange, the industry will write lots of Java code to consume XML Schema-based documents. JAXB (Java Architecture for XML Binding) provides a convenient way to bind an XML Schema to a representation in Java code. This makes it easy for developers to incorporate XML data and processing functions in applications based on Java technology without having to know much about XML itself.
This article will show developers how to utilize the power, ease, and flexibility of JAXB from within the WebSphere Studio Application Developer (referred to as WebSphere Studio), while developing real-world enterprise applications. With JAXB, developers no longer need to learn the intricacies of XML parsing techniques!
What You Need to Know
Although developers need not worry about XML parsing techniques anymore, they may need to do some reading up.
JAXB is built upon XML Schema, so a sound understanding of XML Schema is imperative in order to harness the power of JAXB. Business domain objects and their structural relationships can be well represented through an XML Schema, provided you have a good understanding of its power. The essence of JAXB lies in its customization of the XML Schema for Java objects. These customizations follow the rules of XML Schema. Adding JAXB customizations to a business domain object model (represented in XML Schema) is simple once the hard work of creating the schema is accomplished.
This article assumes you understand:
- XML Schemas (the more detailed the better)
- WebSphere Studio 5.1
You must have the following products to complete the steps in this article:
- WebSphere Studio Application Developer 5.1
- Java Web Services Developer's Pack (JWSDP v1.3)
JWSDP from Sun Microsystems is a free integrated toolkit you can use to build, test, and deploy XML applications, Web services, and Web applications with the latest Web services technologies and standards implementations. JAXB is an essential part of this package. JWSDP 1.3 must be downloaded and installed (see the Development Environment Setup section for a detailed explanation of the process).
In object-oriented programming (OOP) there are two pivotal concepts: classes and objects. Classes provide the structure for software concepts or entities, whereas objects are live instances of the classes. A similar analogy can be used in XML representation. An XML Schema can be conceptualized as the allowable structure and constructs that can be used in the creation of an XML document (which conforms to the schema). The schema (in XML) and classes (in OOP) are the conceptual building blocks, whereas documents (in XML) and objects (in OOP) are live instances that conform to their respective conceptual building blocks.
Working with Java objects and classes is fundamentally different from working with XML. JAXB brings in the idea of data binding, which creates a correspondence between the XML schemas and the Java classes, and then utilizes the mapping to convert XML documents to and from Java classes. The JAXB schema compiler creates Java classes and interfaces based on the structure of the XML Schema. JAXB libraries are used in marshaling and unmarshaling. Marshaling is the process of turning one or more Java objects into an XML document, whereas unmarshaling is the reverse process - creating a Java object from an XML document.
To use JAXB in a Java application the first step is to run the JAXB compiler (XJC) to create the Java classes and interfaces. For every element in the XML Schema, one or two Java interfaces and corresponding implementation classes (which implement the interfaces) are generated. The implementation classes are generated in a package that is separate from the one in which the interfaces are generated. Other than the interfaces and the implementation classes, JAXB-specific classes are generated to perform marshaling and unmarshaling, and to create instances of the implementation classes, among other things. (Note: The implementation classes are only instantiated through a factory class generated by the JAXB compiler). The two classes that are primarily used in working with JAXB-created Java objects are:
- JAXBContext: Used for marshaling, unmarshaling, and validating XML documents
- ObjectFactory: Used to instantiate the various implementation classes
The first step is to download and install the JWSDP. As a part of JWSDP, the JAXB compile and runtime libraries and executables are installed. The next step is to download the JAXBTestEAR.zip file from www.sys-con.com/websphere/sourcec.cfm and extract the contents into the C:\temp folder. Next, create a new workspace and import the JAXBTestEAR file:
- Create a folder called JAXB and another called workspace under JAXB in the C:\ drive.
- Open WebSphere Studio and point the workspace to C:\JAXB\workspace.
- Select File>Import from the WebSphere Studio menu.
- Select the EAR file type to import (see Figure 1).
- Browse the file system and select the JAXBTestEAR.ear file. Click Finish.
- The imported project in the J2EE Perspectives, Project Navigator tab should look like Figure 2.
- Select External Tools from the WebSphere Studio menu as shown in Figure 3.
- Click New to create a new configuration. In the Main tab, key in the Name, Location, Working Directory, and Arguments as shown in Figure 4. (Browse the File system and Workspace in order to obtain the values.)
- Switch to the Resource tab and check the checkboxes. Highlight the $resource Scope Variable and then choose JAXBTest as the specific resource.
- Click Apply to save the configurations. Click Run to run the JAXB compiler (xjc.bat) and generate the Java classes and interfaces. The output in the output console will resemble Figure 5.
The example we will develop is a simple GUI menuing system. A widget is a conceptual element that defines the whole set of drawing components that constitute a graphical user interface. Examples of widgets are rectangles, squares, circles, and any other drawable visual component. Our simplistic GUI consists of three visual components in the form of rectangles, squares, and circles. The Widgets component encapsulates a list of the visual components (rectangles, squares, and circles) in any random order. A client can construct the visual components from a given XML document and then retrieve the details of each visual component (to be used in any way the client requires).
Listing 1 illustrates the XML Schema (for the example scenario) in some detail. The xs:annotation element is a container for the xs:appinfo and xs:documentation elements, which contain additional information. These two elements are dedicated to holding machine-processable (xs:appinfo) information and human-readable documentation (xs:documentation).
The section highlighted in blue denotes the JAXB-specific global customizations of the schema that are applicable to the entire schema file. The section highlighted in red denotes the JAXB-specific customizations in which the package where the generated files will be placed is defined (in this case, com.ibm.domainobjects).
In the globalBindings section the collectionType = java.util.ArrayList is highlighted in blue. It denotes that any Collections that are created inside the Java objects all conform to the List interface in Java and are actual instances of ArrayList.
The xjc:serializable element makes sure that all the generated classes are serializable. (This is particularly important when objects are sent and received over the wire while data transfer between various application tiers is performed.)
A superClass called Shape is also defined. This denotes that all the elements defined in this schema have a common superclass called Shape. I show this here in order to illustrate that the classes that are created from the schema can also refer to external classes, i.e., classes that are not created by the JAXB compiler.
The definition of the Widgets element shows that there is a property called "Widgets" that is defined. Had this property not been defined, the binding compiler would have autogenerated a name for the list of choices (for the rectangles, squares, and circles). The autogeneration usually takes the names of the subelements and combines them with "Or". Hence the accessor name for the list would have been something like:
Clearly this is not meaningful. The jxb:property name="Widgets" denotes that the accessor method will be getWidgets, which is clearly much more intuitive than the autogenerated name.
In the example, I will demonstrate:
- How to set up the usage of the external JAXB compiler from WebSphere Studio
- How to use the JAXB compiler to generate Java interfaces and classes
- How to create the Java classes from an XML document
- How to instantiate Java classes, set their attributes, and then generate the corresponding XML document (the reverse of the previous step)
Using the JAXB Generated Classes
The JAXB compiler is run only once. There is no need to run it again if the schema is not changed. However, if the schema undergoes any changes, then the JAXB compiler preprocessing step must be executed again in order to regenerate the Java interfaces and classes. In this event, it is advisable to delete all of the generated classes from a previous run of the JAXB compiler before running this step again. This is particularly helpful in cases in which elements in the schema are deleted. The generated classes from the new run of the compiler do not delete the old generated classes and hence some unused classes from the previous version of the schema will be left behind.
Once all the classes are generated, the fun begins, with the bulk of the tedious (if you consider it so) setup work already completed. It is time to use the generated classes in order to convert XML documents into Java classes and vice versa. We will concentrate on a client class called JAXBTester. This class demonstrates two activities. It first reads in a given XML document from which it creates the Java object structure/tree.
Analysis of JAXBTester
JAXBTester has three methods. A brief explanation of each of the methods is in order.
Notice how not a single line of XML-specific code needs to be coded by the developer; the specifics of the conversion are all contained inside the generated classes.
Running the Sample
Before running the client class several steps must be followed in sequence:
- The JAXB compiler must be run to generate the Java object tree. (This step should already have been completed.)
- The import statements that are commented out in the JAXBTester must be uncommented. The method bodies of unmarshallIt and marshallIt that are commented out need to be uncommented prior to compilation of JAXBTester. These portions are commented out because the classes that are referenced (both in the import statements and inside the methods) are all generated classes. When the EAR file was imported, these generated classes did not exist; hence, prior to Step 1 JAXBTester would not have compiled.
- The value of the absPathToXML, which denotes the path to the schema.xml file, must be changed (if required) to reflect the path to the file in the file system.
This article introduced you to the basics of JAXB. The most important aspect of this article was the demonstration of how JAXB can be used in a J2EE application and how the all development can be achieved from our friendly and most loved IDE - WebSphere Studio.
The best way to learn JAXB is to get very familiar and comfortable with XML Schema and then learn the tricks of JAXB customizations. The best way to learn XML Schema is to read a book on it, and then apply the concepts in real-world scenarios, exploiting the various features. (The O'Reilly book XML Schema is my favorite.)
SYS-CON Events announced today that Vicom Computer Services, Inc., a provider of technology and service solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. They are located at booth #427. Vicom Computer Services, Inc. is a progressive leader in the technology industry for over 30 years. Headquartered in the NY Metropolitan area. Vicom provides products and services based on today’s requirements around Unified Networks, Cloud Computing strategies, Virtualization around Software defined Data Ce...
Apr. 18, 2015 02:00 PM EDT Reads: 1,349
SYS-CON Events announced today that Optimal Design, an Internet of Things solution provider, will exhibit at SYS-CON's Internet of @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Optimal Design is an award winning product development firm offering industrial design and engineering services to the consumer, medical, and defense markets.
Apr. 18, 2015 01:45 PM EDT Reads: 1,579
SYS-CON Events announced today that Tufin, the market-leading provider of Security Policy Orchestration Solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. As the market leader of Security Policy Orchestration, Tufin automates and accelerates network configuration changes while maintaining security and compliance. Tufin's award-winning Orchestration Suite™ gives IT organizations the power and agility to enforce security policy across complex, multi-vendor enterprise networks. With more than 1...
Apr. 18, 2015 01:45 PM EDT Reads: 3,246
SYS-CON Events announced today that MangoApps will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY., and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. MangoApps provides private all-in-one social intranets allowing workers to securely collaborate from anywhere in the world and from any device. Social, mobile, and easy to use. MangoApps has been named a "Market Leader" by Ovum Research and a "Cool Vendor" by Gartner...
Apr. 18, 2015 01:15 PM EDT Reads: 3,533
SYS-CON Events announced today that Cloudian, Inc., the leading provider of hybrid cloud storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Cloudian, Inc., is a Foster City, California - based software company specializing in cloud storage software. The main product is Cloudian, an Amazon S3-compliant cloud object storage platform, the bedrock of cloud computing systems, that enables cloud service providers and enterprises to build reliable, affordable and scalable cloud storage solu...
Apr. 18, 2015 01:00 PM EDT Reads: 2,317
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
Apr. 18, 2015 12:45 PM EDT Reads: 4,180
SYS-CON Events announced today that Creative Business Solutions will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Creative Business Solutions is the top stocking authorized HP Renew Distributor in the U.S. Based out of Long Island, NY, Creative Business Solutions offers a one-stop shop for a diverse range of products including Proliant, Blade and Industry Standard Servers, Networking, Server Options and Care Packs. As a trusted supplier, CBS guarantees quality controlled stock levels thanks to an Auto...
Apr. 18, 2015 12:30 PM EDT Reads: 4,233
How is unified communications transforming the way businesses operate? In his session at WebRTC Summit, Arvind Rangarajan, Director of Product Marketing at BroadSoft, will discuss how to extend unified communications experience outside the enterprise through WebRTC. He will also review use cases across different industry verticals. Arvind Rangarajan is Director, Product Marketing at BroadSoft. He has over 19 years of experience in the telecommunications industry in various roles such as Software Development, Product Management and Product Marketing, applied across Wireless, Unified Communic...
Apr. 18, 2015 12:30 PM EDT Reads: 1,570
SYS-CON Events announced today that IDenticard will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada. Our partner network encompasses some 300 of the world's leading systems integrators and security s...
Apr. 18, 2015 12:00 PM EDT Reads: 4,878
What exactly is a cognitive application? In her session at 16th Cloud Expo, Ashley Hathaway, Product Manager at IBM Watson, will look at the services being offered by the IBM Watson Developer Cloud and what that means for developers and Big Data. She'll explore how IBM Watson and its partnerships will continue to grow and help define what it means to be a cognitive service, as well as take a look at the offerings on Bluemix. She will also check out how Watson and the Alchemy API team up to offer disruptive APIs to developers.
Apr. 18, 2015 12:00 PM EDT Reads: 1,422
The IoT Bootcamp is coming to Cloud Expo | @ThingsExpo on June 9-10 at the Javits Center in New York. Instructor. Registration is now available at http://iotbootcamp.sys-con.com/ Instructor Janakiram MSV previously taught the famously successful Multi-Cloud Bootcamp at Cloud Expo | @ThingsExpo in November in Santa Clara. Now he is expanding the focus to Janakiram is the founder and CTO of Get Cloud Ready Consulting, a niche Cloud Migration and Cloud Operations firm that recently got acquired by Aditi Technologies. He is a Microsoft Regional Director for Hyderabad, India, and one of the f...
Apr. 18, 2015 12:00 PM EDT Reads: 934
The 17th International Cloud Expo has announced that its Call for Papers is open. 17th International Cloud Expo, to be held November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA, brings together Cloud Computing, APM, APIs, Microservices, Security, Big Data, Internet of Things, DevOps and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding business opportunity. Submit your speaking proposal today!
Apr. 18, 2015 12:00 PM EDT Reads: 2,051
With IoT exploding, massive data will transform businesses with opportunities to monetize almost anything that can be measured. In this C-Level Roundtable Discussion at @ThingsExpo, Brendan O’Brien, Aria Systems Co-founder and Chief Evangelist, will lead an expert panel of consultants, thought leaders and practitioners who will look at these new monetization trends, discuss the implications, and detail lessons learned from their collective experience. Finally, the panel will point the way forward for enterprises who wish to leverage the resulting complex recurring revenue models, adding valu...
Apr. 18, 2015 11:00 AM EDT Reads: 1,409
SYS-CON Events announced today that Ciqada will exhibit at SYS-CON's @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Ciqada™ makes it easy to connect your products to the Internet. By integrating key components - hardware, servers, dashboards, and mobile apps - into an easy-to-use, configurable system, your products can quickly and securely join the internet of things. With remote monitoring, control, and alert messaging capability, you will meet your customers' needs of tomorrow - today! Ciqada. Let your products take flight. For more inform...
Apr. 18, 2015 07:00 AM EDT Reads: 1,567
SYS-CON Events announced today that GENBAND, a leading developer of real time communications software solutions, has been named “Silver Sponsor” of SYS-CON's WebRTC Summit, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. The GENBAND team will be on hand to demonstrate their newest product, Kandy. Kandy is a communications Platform-as-a-Service (PaaS) that enables companies to seamlessly integrate more human communications into their Web and mobile applications - creating more engaging experiences for their customers and boosting collaboration and productiv...
Apr. 18, 2015 06:00 AM EDT Reads: 2,241
SYS-CON Events announced today that BroadSoft, the leading global provider of Unified Communications and Collaboration (UCC) services to operators worldwide, has been named “Gold Sponsor” of SYS-CON's WebRTC Summit, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. BroadSoft is the leading provider of software and services that enable mobile, fixed-line and cable service providers to offer Unified Communications over their Internet Protocol networks. The Company’s core communications platform enables the delivery of a range of enterprise and consumer calling...
Apr. 18, 2015 05:30 AM EDT Reads: 2,170
VoxImplant has announced full WebRTC support in the newest versions of its Android SDK and iOS SDK. The updated SDKs, which enable audio and video calls on mobile devices, are now compatible with the WebRTC standard to allow any mobile app to communicate with WebRTC-enabled browsers, including Google Chrome, Mozilla Firefox, Opera, and, when available, Microsoft Spartan. The WebRTC-updated SDKs represent VoxImplant's continued leadership in simplifying the development of real-time communications (RTC) services for app developers. VoxImplant (built by Zingaya, the real-time communication servi...
Apr. 18, 2015 02:45 AM EDT Reads: 1,794
SYS-CON Events announced today that AIC, a leading provider of OEM/ODM server and storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. AIC is a leading provider of both standard OTS, off-the-shelf, and OEM/ODM server and storage solutions. With expert in-house design capabilities, validation, manufacturing and production, AIC's broad selection of products are highly flexible and are configurable to any form factor or custom configuration. AIC leads the industry with nearly 20 years of ...
Apr. 18, 2015 12:15 AM EDT Reads: 4,663
Buzzword alert: Microservices and IoT at a DevOps conference? What could possibly go wrong? Join this panel of experts as they peel away the buzz and discuss the important architectural principles behind implementing IoT solutions for the enterprise. As remote IoT devices and sensors become increasingly intelligent, they become part of our distributed cloud environment, and we must architect and code accordingly. At the very least, you’ll have no problem filling in your buzzword bingo cards.
Apr. 16, 2015 05:30 PM EDT Reads: 2,101
Internet of Things (IoT) will be a hybrid ecosystem of diverse devices and sensors collaborating with operational and enterprise systems to create the next big application. In their session at @ThingsExpo, Bramh Gupta, founder and CEO of robomq.io, and Fred Yatzeck, principal architect leading product development at robomq.io, will discuss how choosing the right middleware and integration strategy from the get-go will enable IoT solution developers to adapt and grow with the industry, while at the same time reduce Time to Market (TTM) by using plug and play capabilities offered by a robust I...
Apr. 13, 2015 12:15 PM EDT Reads: 1,875