Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

7 Steps to Solve Real World Problem using JAXB (Java API for XML Binding)

0.00/5 (No votes)
11 Jul 2011CPOL4 min read 26.9K   321  
This article will discuss about the real world problem and then we will see that how it can be resolved using JAXB.

Introduction

In this article, we are going to look at a real world problem and then see how it can be resolved using JAXB. Click the following link and view my other videos on Java Design Pattern, J2EE, Hibernate, Struts 2, Spring and Webservices.

Problem

Maintaining quality for a software application is a major challenge for most of the managers. If the quality itself is a requirement, then implementing major quality solutions involving integration of quality tools with Continuous integration environment will be useful. This is quality at the top approach. But many projects may not get this advantage and managers may wonder how to ensure quality of projects without totally devoting their time to it but at the same time ensuring it is not under looked. Along with proof of quality always comes handy.

Image 1

Quality check using Findbugs

As I work more on Java/JEE technology, I will take you through a proposed solution towards them but the same can also be implemented with Microsoft technologies.

Findbugs is a good tool for performing analysis of Java code and it lists down problems at various part of code which can cause problems with respect to functioning, performance undelivered and at the same time maintainability of the code.

Hence most of the Java/JEE world quality driven applications use the same for code analysis.

The advantage of Findbugs is that it can generate output in various format like XML, HTML and so on.

Then What’s the Problem

Findbugs can run analysis and produce reports, but the challenge is to see how code quality behaves over time, i.e., whether quality is degraded over improved.

If by some means we can read the XML and then persist some of the important results of analysis like High priority bug, project name as well as date of analysis, then the same can be used to generate reports which will indicate the behavior of code quality.

There are various possible ways in which this can be achieved but as my article concentrates on JAXB, I will implement the solution using JAXB.

Proposed Solution

Image 2

The above diagram shows the proposed solution by using Findbugs, analysis is performed on the source code and the analysis output is stored in the forms of an XML. We name the same as findbugs.xml.

Now using JAXB, we bind the XML to Content Objects.

Important fields pertaining to the analysis like high priority bugs, project name and time stamp are stored in the database and once persisted these can be used to provide report showing over all behavior of code quality.

In this example, we will see how to bind the values of XML using JAXB and then create a small application which can retrieve values for XML.

Why JAXB?

JAXB binds a XML with corresponding content objects and this is what is required in the above example. We could have used JAXP but then parsing complexities would have added to overhead and hence we preferred JAXB.

Implementing Solution

Step 1: Writing XSD for findbugs. We will create an xsd mapping to findbugs.xml. A simple snapshot of the xsd is shown. Detailed xsd is attached.

Image 3

Step 2: Open Eclipse and create a Java project.

Image 4

Name the project as FindbugsJAXB.

Image 5

Step 3: Create a folder named XML within the project and copy the findbugs.xml which is generated after analysis. To know how to generate the same, refer to the following link:

Image 6

Similarly make a folder named xsd and copy the xsd which we created in Step 1.

Step 4: Open command prompt and go to the above created xsd folder. Execute the following xjc command:

xjc –p package xsdname 
for e.g. xjc –p com.questpond.generatedClasses findbugs.xsd

Image 7

The above command will generate the content objects classes. Copy the same to the src folder of the project in eclipse.

Image 8

Step 5: Create a Main class and write the following unmarshall method:

Java
public BugCollection unmarshal( Class docClass, InputStream inputStream )
    throws JAXBException {
    String packageName = docClass.getPackage().getName();
    JAXBContext jc = JAXBContext.newInstance( packageName );
    Unmarshaller u = jc.createUnmarshaller();
    BugCollection doc= (BugCollection) u.unmarshal( inputStream );
    return doc;
}

Step 6: Write the following code in the main method of Main class.

Java
public static void main(String args[]){
Main obj = new Main();
try {
BugCollection bugs = obj.unmarshal(BugCollection.class,
	new FileInputStream( new File("F:/JAXBFindbug/FindBugsJAXB/xml/findbugs.xml")));
		FindBugsSummary summary =bugs.getFindBugsSummary();	
		Integer priority1=summary.getPriority1();
		Integer priority2 =summary.getPriority2();
		Integer priority3=summary.getPriority3();
		String timestamp =summary.getTimestamp();
		String projectName=bugs.getProject().getProjectName();
		System.out.println("Project name : "+projectName);
		System.out.println("High Impact Bugs:"+priority1);
		System.out.println("Medium Impact Bugs: "+priority2);
		System.out.println("Low Impact Bugs: "+priority3);
		System.out.println("Time when analysis was done"+timestamp);
		System.out.println();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JAXBException e) {
			e.printStackTrace();
		}		
	}

In the above code, we unmarshall our XML by providing the path of the XML to the unmarshall function.

Step 7: On executing the above code, we will get output displaying project name, bugs as well as timestamp.

Image 9

The above read values can be persisted in database using JDBC connection and a report can be pulled from the same by providing from date and to date to see a graph showing the behavior of code quality.

What’s Up for Microsoft World?

FxCorp is code quality tool in Microsoft world analogous to Findbugs in Java world.

The same application can also be implemented by using FxCorp for analysis of source code in the Microsoft world.

We can also have an Integrated center point for both Microsoft and Java by integrating both FXCorp and Findbugs so that at any point of time we can analyze code quality of applications independent of technology.

Image 10

Conclusion

JAXB is widely used in Web services for marshalling and unmarshalling XML and content objects. Along with this use, JAXB can also be used in number of other scenarios where binding an XML is required to either process it or persist it.

Its ease of use and flexibility is a major advantage while dealing with XML’s processing.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)