|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.)
Can call centers hang up the phones for good? Intuitive Solutions did. WebRTC enabled this contact center provider to eliminate antiquated telephony and desktop phone infrastructure with a pure web-based solution, allowing them to expand beyond brick-and-mortar confines to a home-based agent model. It also ensured scalability and better service for customers, including MUY! Companies, one of the country's largest franchise restaurant companies with 232 Pizza Hut locations. This is one example of WebRTC adoption today, but the potential is limitless when powered by IoT.
May. 27, 2015 09:00 PM EDT Reads: 6,767
One of the biggest challenges when developing connected devices is identifying user value and delivering it through successful user experiences. In his session at Internet of @ThingsExpo, Mike Kuniavsky, Principal Scientist, Innovation Services at PARC, described an IoT-specific approach to user experience design that combines approaches from interaction design, industrial design and service design to create experiences that go beyond simple connected gadgets to create lasting, multi-device experiences grounded in people's real needs and desires.
May. 27, 2015 09:00 PM EDT Reads: 6,397
The true value of the Internet of Things (IoT) lies not just in the data, but through the services that protect the data, perform the analysis and present findings in a usable way. With many IoT elements rooted in traditional IT components, Big Data and IoT isn’t just a play for enterprise. In fact, the IoT presents SMBs with the prospect of launching entirely new activities and exploring innovative areas. CompTIA research identifies several areas where IoT is expected to have the greatest impact.
May. 27, 2015 09:00 PM EDT Reads: 5,328
The Internet of Things will greatly expand the opportunities for data collection and new business models driven off of that data. In her session at @ThingsExpo, Esmeralda Swartz, CMO of MetraTech, discussed how for this to be effective you not only need to have infrastructure and operational models capable of utilizing this new phenomenon, but increasingly service providers will need to convince a skeptical public to participate. Get ready to show them the money!
May. 27, 2015 07:00 PM EDT Reads: 6,284
SYS-CON Events announced today that MetraTech, now part of Ericsson, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Ericsson is the driving force behind the Networked Society- a world leader in communications infrastructure, software and services. Some 40% of the world’s mobile traffic runs through networks Ericsson has supplied, serving more than 2.5 billion subscribers.
May. 27, 2015 06:45 PM EDT Reads: 2,209
The Internet of Things is not only adding billions of sensors and billions of terabytes to the Internet. It is also forcing a fundamental change in the way we envision Information Technology. For the first time, more data is being created by devices at the edge of the Internet rather than from centralized systems. What does this mean for today's IT professional? In this Power Panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists will addresses this very serious issue of profound change in the industry.
May. 27, 2015 06:00 PM EDT Reads: 1,485
SYS-CON Events announced today that BMC 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. BMC delivers software solutions that help IT transform digital enterprises for the ultimate competitive business advantage. BMC has worked with thousands of leading companies to create and deliver powerful IT management services. From mainframe to cloud to mobile, BMC pairs high-speed digital innovation with robust IT industrialization – allowing customers to provide amazing user experiences with optimized IT per...
May. 27, 2015 05:30 PM EDT Reads: 1,230
The Internet of Things is not new. Historically, smart businesses have used its basic concept of leveraging data to drive better decision making and have capitalized on those insights to realize additional revenue opportunities. So, what has changed to make the Internet of Things one of the hottest topics in tech? In his session at @ThingsExpo, Chris Gray, Director, Embedded and Internet of Things, discussed the underlying factors that are driving the economics of intelligent systems. Discover how hardware commoditization, the ubiquitous nature of connectivity, and the emergence of Big Data a...
May. 27, 2015 04:00 PM EDT Reads: 6,817
The world is at a tipping point where the technology, the device and global adoption are converging to such a point that we will see an explosion of a world where smartphone devices not only allow us to talk to each other, but allow for communication between everything – serving as a central hub from which we control our world – MediaTek is at the heart of both driving this and allowing the markets to drive this reality forward themselves. The next wave of consumer gadgets is here – smart, connected, and small. If your ambitions are big, so are ours. In his session at @ThingsExpo, Jack Hu, D...
May. 27, 2015 12:49 PM EDT Reads: 793
SYS-CON Events announced today that DragonGlass, an enterprise search platform, 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. After eleven years of designing and building custom applications, OpenCrowd has launched DragonGlass, a cloud-based platform that enables the development of search-based applications. These are a new breed of applications that utilize a search index as their backbone for data retrieval. They can easily adapt to new data sets and provide access to both structured and unstruc...
May. 27, 2015 12:00 PM EDT Reads: 2,386
The 4th International Internet of @ThingsExpo, co-located with the 17th International Cloud Expo - to be held November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA - announces that its Call for Papers is open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
May. 27, 2015 12:00 PM EDT Reads: 2,447
We’re entering a new era of computing technology that many are calling the Internet of Things (IoT). Machine to machine, machine to infrastructure, machine to environment, the Internet of Everything, the Internet of Intelligent Things, intelligent systems – call it what you want, but it’s happening, and its potential is huge. IoT is comprised of smart machines interacting and communicating with other machines, objects, environments and infrastructures. As a result, huge volumes of data are being generated, and that data is being processed into useful actions that can “command and control” thi...
May. 27, 2015 11:51 AM EDT Reads: 777
As the Internet of Things unfolds, mobile and wearable devices are blurring the line between physical and digital, integrating ever more closely with our interests, our routines, our daily lives. Contextual computing and smart, sensor-equipped spaces bring the potential to walk through a world that recognizes us and responds accordingly. We become continuous transmitters and receivers of data. In his session at @ThingsExpo, Andrew Bolwell, Director of Innovation for HP's Printing and Personal Systems Group, discussed how key attributes of mobile technology – touch input, sensors, social, and ...
May. 27, 2015 11:30 AM EDT Reads: 4,402
All major researchers estimate there will be tens of billions devices - computers, smartphones, tablets, and sensors - connected to the Internet by 2020. This number will continue to grow at a rapid pace for the next several decades. With major technology companies and startups seriously embracing IoT strategies, now is the perfect time to attend @ThingsExpo, June 9-11, 2015, at the Javits Center in New York City. Learn what is going on, contribute to the discussions, and ensure that your enterprise is as "IoT-Ready" as it can be
May. 27, 2015 11:15 AM EDT Reads: 2,865
WebRTC defines no default signaling protocol, causing fragmentation between WebRTC silos. SIP and XMPP provide possibilities, but come with considerable complexity and are not designed for use in a web environment. In his session at @ThingsExpo, Matthew Hodgson, technical co-founder of the Matrix.org, discussed how Matrix is a new non-profit Open Source Project that defines both a new HTTP-based standard for VoIP & IM signaling and provides reference implementations.
May. 27, 2015 10:30 AM EDT Reads: 5,525
Buzzword alert: Microservices and IoT at a DevOps conference? What could possibly go wrong? In this Power Panel at DevOps Summit, moderated by Jason Bloomberg, the leading expert on architecting agility for the enterprise and president of Intellyx, panelists will 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 fil...
May. 27, 2015 10:00 AM EDT Reads: 2,301
"People are a lot more knowledgeable about APIs now. There are two types of people who work with APIs - IT people who want to use APIs for something internal and the product managers who want to do something outside APIs for people to connect to them," explained Roberto Medrano, Executive Vice President at SOA Software, in this SYS-CON.tv interview at Cloud Expo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
May. 27, 2015 09:30 AM EDT Reads: 4,704
Almost everyone sees the potential of Internet of Things but how can businesses truly unlock that potential. The key will be in the ability to discover business insight in the midst of an ocean of Big Data generated from billions of embedded devices via Systems of Discover. Businesses will also need to ensure that they can sustain that insight by leveraging the cloud for global reach, scale and elasticity.
May. 27, 2015 09:30 AM EDT Reads: 7,190
In their session at @ThingsExpo, Shyam Varan Nath, Principal Architect at GE, and Ibrahim Gokcen, who leads GE's advanced IoT analytics, focused on the Internet of Things / Industrial Internet and how to make it operational for business end-users. Learn about the challenges posed by machine and sensor data and how to marry it with enterprise data. They also discussed the tips and tricks to provide the Industrial Internet as an end-user consumable service using Big Data Analytics and Industrial Cloud.
May. 27, 2015 07:30 AM EDT Reads: 5,765
Building low-cost wearable devices can enhance the quality of our lives. In his session at Internet of @ThingsExpo, Sai Yamanoor, Embedded Software Engineer at Altschool, provided an example of putting together a small keychain within a $50 budget that educates the user about the air quality in their surroundings. He also provided examples such as building a wearable device that provides transit or recreational information. He then reviewed the resources available to build wearable devices at home including open source hardware, the raw materials required and the options available to power s...
May. 27, 2015 04:30 AM EDT Reads: 4,411