|By Tilak Mitra||
|April 5, 2004 12:00 AM EDT||
It has been well proven over the past few years that the best form of information exchange (in a typical B2B and B2C environment) is through XML. There are various XML-based standards (schema) for both the horizontal and vertical market sectors and there are ongoing efforts to move toward a standardized format in the various industry sectors.
With the proliferation of an XML-based information exchange, the industry is bound to write lots of Java code to consume XML Schema-based documents. Java Architecture for XML Binding (JAXB) provides a convenient way to bind an XML Schema to a representation in Java code, making it easy for developers to incorporate XML data and processing functions in applications based on Java technology without having to know much about the details of XML parsing.
How It Works
The use of JAXB starts from an XML Schema. Typically in an enterprise application, an XML Schema is defined that constitutes the business domain objects and their interrelationships.
The JAXB (binding) compiler creates a set of classes and interfaces from the XML Schema (see Figure 1). These sets of classes and interfaces are referenced and used in the application. The application developer has a rich set of JAXB APIs that he or she uses to convert a Java object tree/ structure (made up of the instances of classes generated by the binding compiler) to an XML document (that conforms to the XML Schema). The process of converting an XML document into a Java object tree is as seamless and easy as the former and the beauty of it all is that the developer does not have to write a single line of XML parsing routines in either of the conversion processes.
The process of converting a Java Object Tree to an XML document is known as marshalling, whereas the reverse process of converting an XML document to a Java Object Tree is called unmarshalling.
The process of creating the classes and interfaces from the XML Schema utilizes the JAXB binding compiler (xjc.bat or xjc.sh) that's included with the installation (see Resources section).
xjc.bat -p <package name> -d <working directory>
The -p option specifies the Java package for the generated classes and interfaces while the -d option specifies the working directory.
Once the classes and interfaces are generated, they can be used in the enterprise application. There are two typical usage scenarios.
1. Unmarshal an XML document to a Java object tree
JAXBContext jContext = JAXBContext.newInstance("<package name>") ;
where package name contains the JAXB generated classes.
Unmarshaller unmarshaller = jContext.createUnmarshaller() ;
Library library = (Library)unmarshaller.unmarshal(new
library.xml is an example XML document that conforms to the schema file from which the JAXB-generated Java classes and interfaces are created, and Library is the root object in the XML document.
Once a handle to the Library object instance is obtained, we're in the Java universe! The developer can use the power of Java to traverse through the object tree and use the same in the application, as required.
2. Marshal a Java object tree to an XML document
A JAXB implementation-provided class called ObjectFactory is used to create instances of the classes and interfaces that are generated by the JAXB binding compiler (during the generation process). This class uses the Factory pattern to create instances of the generated classes and this is the only way the class instances may be created.
Consider a small example in which a Library contains a list of Books in which each book has a title and a price field. The steps to create an XML document from the Java object tree are as follows:
ObjectFactory factory = new ObjectFactory() ;
Library library = factory.createLibrary() ;
Book bookOne = factory.createBook() ;
bookOne.setTitle("Design Patterns") ;
Book bookTwo = factory.createBook() ;
bookTwo.setTitle("Analysis Patterns") ;
Marshaller marshaller = jContext.createMarshaller() ;
marshaller.marshal(library, new FileOutputStream("library.xml")) ;
The power and flexibility of JAXB is further augmented by its customization feature that's added on top of the schema bindings. To add specific functionality to an application, JAXB binding customizations are used. These customizations are read and interpreted by the JAXB compiler. Customization is affected by annotating a schema with binding declarations that either override or extend the default bindings.
Customization has four scopes:
Although a detailed discussion about customization is beyond the scope of this article, it's worthwhile mentioning a few customization artifacts that are used more frequently in a typical JAXB usage scenario.
Customization bindings can be made at a global level of declaration that applies to all the defined elements in the XML Schema. Listing 1 provides a typical global customization binding.
Notice how in the listing defining the collectionType attribute tells the compiler that the type of collection used in the generated classes is of type ArrayList. The <xjc: serializable> element ensures that all the generated classes implement the Java Serializable marker interface. The <jxb: package> element's name attribute denotes the Java package in which the generated classes and interfaces are created.
All these elements' attributes can be tuned and configured to suit the requirements of the application.
Customization of the default binding can also be made at the element and its attribute's level. For example, an element can be adorned with its Javadoc by using annotations. Custom property names can also be specified that when defined, generate getter and setter methods for the property. This is illustrated in Listing 2. (Listing 2 can be downloaded from www.sys-con.com/java/sourcec.cfm.)
Notice how in the listing the <jxb: javadoc> element is used to create the documentation for the generated class. The <jxb:property> element is used to name an instance variable inside the generated class (Library class in this case) and generate its getter and setter methods. The generated class hence will have two methods: getBookList() and setBookList(...).
A detailed explanation of these bindings can be found in the Resources section (JAXB User's Guide).
Thinking in Terms of JAXB
In a typical enterprise application that's comprised of various application tiers, data is exchanged between these tiers to realize the business functionalities. It's a good design principle to create a data object model that can be used for inter-tier data exchange. This data object model is a good candidate to be represented in an XML Schema with JAXB customizations. With this design in place, data can be exchanged in either XML format or as Java objects while the JAXB libraries can be used to convert between XML and Java in a seamless fashion.
This article introduced the basic concepts of JAXB, how it works, and how it can be used in a Java-based enterprise application. It also provided a sneak peek at how JAXB customizations can be used to tailor an XML Schema to conform to the application requirements.
Part 2 will take a concrete example of an XML Schema and discuss the process of generating the classes and interfaces from a valid XML document and also the reverse process of creating an XML document from a Java object tree. Stay tuned!
(Note: The Reference Implementation of JAXB comes packaged inside the Java Web Services Developer's Pack [JWSDP]. Once this is installed, the JAXB compile time and runtime libraries are available in the <install-root>\jaxb directory.)
The Open Compute Project is a collective effort by Facebook and a number of players in the datacenter industry to bring lessons learned from the social media giant's giant IT deployment to the rest of the world. Datacenters account for 3% of global electricity consumption – about the same as all of Switzerland or the Czech Republic -- according to people I met at the recent Open Compute Summit in San Jose. With increasing mobility at the edge of the cloud and vast new dataflows being predicted with the growth of the Internet of Things (and The Coming Age of Many Zettabytes) in the near...
Mar. 28, 2015 01:00 AM EDT Reads: 1,775
GENBAND has announced that SageNet is leveraging the Nuvia platform to deliver Unified Communications as a Service (UCaaS) to its large base of retail and enterprise customers. Nuvia’s cloud-based solution provides SageNet’s customers with a full suite of business communications and collaboration tools. Two large national SageNet retail customers have recently signed up to deploy the Nuvia platform and the company will continue to sell the service to new and existing customers. Nuvia’s capabilities include HD voice, video, multimedia messaging, mobility, conferencing, Web collaboration, deskt...
Mar. 28, 2015 01:00 AM EDT Reads: 1,384
The list of ‘new paradigm’ technologies that now surrounds us appears to be at an all time high. From cloud computing and Big Data analytics to Bring Your Own Device (BYOD) and the Internet of Things (IoT), today we have to deal with what the industry likes to call ‘paradigm shifts’ at every level of IT. This is disruption; of course, we understand that – change is almost always disruptive.
Mar. 27, 2015 08:15 PM EDT Reads: 999
SYS-CON Events announced today that Cisco, the worldwide leader in IT that transforms how people connect, communicate and collaborate, has been named “Gold 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 City, NY. Cisco makes amazing things happen by connecting the unconnected. Cisco has shaped the future of the Internet by becoming the worldwide leader in transforming how people connect, communicate and collaborate. Cisco and our partners are building the platform for the Internet of Everything by connecting the...
Mar. 27, 2015 07:00 PM EDT Reads: 5,113
SYS-CON Media announced today that @WebRTCSummit Blog, the largest WebRTC resource in the world, has been launched. @WebRTCSummit Blog offers top articles, news stories, and blog posts from the world's well-known experts and guarantees better exposure for its authors than any other publication. @WebRTCSummit Blog can be bookmarked ▸ Here @WebRTCSummit conference site can be bookmarked ▸ Here
Mar. 27, 2015 06:00 PM EDT Reads: 1,667
Temasys has announced senior management additions to its team. Joining are David Holloway as Vice President of Commercial and Nadine Yap as Vice President of Product. Over the past 12 months Temasys has doubled in size as it adds new customers and expands the development of its Skylink platform. Skylink leads the charge to move WebRTC, traditionally seen as a desktop, browser based technology, to become a ubiquitous web communications technology on web and mobile, as well as Internet of Things compatible devices.
Mar. 27, 2015 06:00 PM EDT Reads: 1,708
SYS-CON Events announced today that robomq.io 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. robomq.io is an interoperable and composable platform that connects any device to any application. It helps systems integrators and the solution providers build new and innovative products and service for industries requiring monitoring or intelligence from devices and sensors.
Mar. 27, 2015 06:00 PM EDT Reads: 1,339
Docker is an excellent platform for organizations interested in running microservices. It offers portability and consistency between development and production environments, quick provisioning times, and a simple way to isolate services. In his session at DevOps Summit at 16th Cloud Expo, Shannon Williams, co-founder of Rancher Labs, will walk through these and other benefits of using Docker to run microservices, and provide an overview of RancherOS, a minimalist distribution of Linux designed expressly to run Docker. He will also discuss Rancher, an orchestration and service discovery platf...
Mar. 27, 2015 04:15 PM EDT Reads: 2,354
Sonus Networks introduced the Sonus WebRTC Services Solution, a virtualized Web Real-Time Communications (WebRTC) offer, purpose-built for the Cloud. The WebRTC Services Solution provides signaling from WebRTC-to-WebRTC applications and interworking from WebRTC-to-Session Initiation Protocol (SIP), delivering advanced real-time communications capabilities on mobile applications and on websites, which are accessible via a browser.
Mar. 27, 2015 04:00 PM EDT Reads: 1,673
SYS-CON Events announced today that Aria Systems, the leading innovator in recurring revenue, has been named “Bronze Sponsor” of SYS-CON's @ThingsExpo, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Proven by the world’s most demanding enterprises, including AAA NCNU, Constant Contact, Falck, Hootsuite, Pitney Bowes, Telekom Denmark, and VMware, Aria helps enterprises grow their recurring revenue businesses. With Aria’s end-to-end active monetization platform, global brands can get to market faster with a wider variety of products and services, while maximizin...
Mar. 27, 2015 04:00 PM EDT Reads: 1,514
SYS-CON Events announced today that Vitria Technology, Inc. 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. Vitria will showcase the company’s new IoT Analytics Platform through live demonstrations at booth #330. Vitria’s IoT Analytics Platform, fully integrated and powered by an operational intelligence engine, enables customers to rapidly build and operationalize advanced analytics to deliver timely business outcomes for use cases across the industrial, enterprise, and consumer segments.
Mar. 27, 2015 03:30 PM EDT Reads: 2,113
SYS-CON Events announced today that Akana, formerly SOA Software, has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. Akana’s comprehensive suite of API Management, API Security, Integrated SOA Governance, and Cloud Integration solutions helps businesses accelerate digital transformation by securely extending their reach across multiple channels – mobile, cloud and Internet of Things. Akana enables enterprises to share data as APIs, connect and integrate applications, drive part...
Mar. 27, 2015 03:15 PM EDT Reads: 1,380
After making a doctor’s appointment via your mobile device, you receive a calendar invite. The day of your appointment, you get a reminder with the doctor’s location and contact information. As you enter the doctor’s exam room, the medical team is equipped with the latest tablet containing your medical history – he or she makes real time updates to your medical file. At the end of your visit, you receive an electronic prescription to your preferred pharmacy and can schedule your next appointment.
Mar. 27, 2015 03:15 PM EDT Reads: 504
The WebRTC Summit 2014 New York, to be held June 9-11, 2015, at the Javits Center in New York, NY, announces that its Call for Papers is open. Topics include all aspects of improving IT delivery by eliminating waste through automated business models leveraging cloud technologies. WebRTC Summit is co-located with 16th International Cloud Expo, @ThingsExpo, Big Data Expo, and DevOps Summit.
Mar. 27, 2015 03:00 PM EDT Reads: 1,372
SYS-CON Events announced today that Solgenia 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. Solgenia is the global market leader in Cloud Collaboration and Cloud Infrastructure software solutions. Designed to “Bridge the Gap” between Personal and Professional Social, Mobile and Cloud user experiences, our solutions help large and medium-sized organizations dr...
Mar. 27, 2015 03:00 PM EDT Reads: 2,675
SYS-CON Events announced today that Liaison Technologies, a leading provider of data management and integration cloud services and solutions, 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. Liaison Technologies is a recognized market leader in providing cloud-enabled data integration and data management solutions to break down complex information barriers, enabling enterprises to make smarter decisions, faster.
Mar. 27, 2015 03:00 PM EDT Reads: 3,363
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - 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.
Mar. 27, 2015 02:00 PM EDT Reads: 2,923
SYS-CON Events announced today that CommVault has been named “Bronze 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 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. A singular vision – a belief in a better way to address current and future data management needs – guides CommVault in the development of Singular Information Management® solutions for high-performance data protection, universal availability and sim...
Mar. 27, 2015 02:00 PM EDT Reads: 1,510
Cloud is not a commodity. And no matter what you call it, computing doesn’t come out of the sky. It comes from physical hardware inside brick and mortar facilities connected by hundreds of miles of networking cable. And no two clouds are built the same way. SoftLayer gives you the highest performing cloud infrastructure available. One platform that takes data centers around the world that are full of the widest range of cloud computing options, and then integrates and automates everything. Join SoftLayer on June 9 at 16th Cloud Expo to learn about IBM Cloud's SoftLayer platform, explore se...
Mar. 27, 2015 02:00 PM EDT Reads: 1,533
SYS-CON Media announced today that 9 out of 10 " most read" DevOps articles are published by @DevOpsSummit Blog. Launched in October 2014, @DevOpsSummit Blog offers top articles, news stories, and blog posts from the world's well-known experts and guarantees better exposure for its authors than any other publication. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce softw...
Mar. 27, 2015 01:00 PM EDT Reads: 2,380