Skip to main content

Posts

Showing posts from July, 2011

Transform Object into XML using JAXB

In the last post we had seen how to transform XML data into user presentable HTML format . What if you don't have data in XML format? What if the data is collected from a legacy system or from a collection of services? Here we are going to look at how to transform the Java object to XML and this XML can be presented in any format we want. Like JAXP, Java comes to rescue us here with JAXB (Java Architecture for XML Binding). All we need is an XSD which defines the structure of the Java Object and the XML. JAXB allows us to transform data in both the direction, means, you can convert Object to XML and XML to Object. Wow, what are we then waiting for? Let's program. Open Netbeans and create a Java Application File->New Project->Java Application Enter "XMLBinding" in the Project Name field Click Finish Create an XSD file Open Projects tab Right click xmlbinding package node Choose XML-> XML Schema Enter "student-marks&quo

Collection of user defined comparable objects (Part 2)

Yesterday we discussed how to make user defined objects comparable to themselves . Let’s take that to one step further today. In yesterdays post, our requirement was to sort the collection of employees that belong to a division, by employee id, so that the end result will be a collection of employees in seniority order. Now there comes the payroll division and it says, for each division, you need to print the list of employees based on the salary drawn. You may say “It is very simple; I had done all the ground work yesterday; so just need to change the comparison logic as below”. public class Employee implements Comparable<Employee> { private String name; private int employeeNo; private float salary; public int compareTo(Employee e) { if (salary == e.salary) { return 0; } else if (salary > e.salary) { return 1; } else { return -1; } } } Now the human resources division shouts “What have you done? Now e

Collection of user defined comparable objects (Part 1)

Let us take array of integers. int[] numbers = new int[] {2,5,4,8,9}; Now, how will you print the numbers in ascending order? You can use Arrays class to sort any type of arrays as follows, java.util.Arrays.sort(numbers); System.out.println(Arrays.toString(numbers)); Your output will be [2, 4, 5, 8, 9] . How can you achieve the same if the data structure is a collection? You can use the class java.util.Collections as follows, List<Integer> numberList = new ArrayList<Integer>(Arrays.asList(2, 5, 4, 8, 9)); Collections.sort(numberList); System.out.println(numberList.toString()); Now assume that you are designing an employee management system. So you will have a class to represent the employee details. Let’s call that class as Employee. class Employee { String name; int employeeNo; ... } Assume that each division maintains a collection of employees. Now you are asked to list the employees in each division in ascending order sorted by employeeNo. You may think what is wro

How to inherit an inner class?

Consider the following example, class A { class B { B(int i) { ... } } ... } Sub-classing B inside class A is very simple because both class B and its sub-class, say C, require the instance of class A and can be done as follows. class A { class B { B(int i) { ... } } class C extends B { C(int i) { super(i); ... } } ... } However, if you want to sub-class B outside class A, as follows, class C extends A.B { C(int i) { super(i); ... } ... } you will get a compiler error as "an enclosing instance that contains A.B is required". In order to solve this problem, you need to get an instance of A, as follows, class C extends A.B { C(A a, int i) { a.super(i); } } Here is the compelete code. public class A { class B { int i; B(int i) { this.i = i; } } } class C extends A.B { C(A a, int i) { a.super(i); } public static void main(String[] args) { A a = new A();

How to use XML and XSL in Java?

Almost everyday, somewhere we come across XML and we know that many systems use XML as their data store; more precisely, they talk in XML language. Now, how do I write an application where I need to present the data, which is in XML format, to users? Is it possible to write XML processing application in Java? Yes, Java SE provides extensive support for XML parsing and processing. Java supports two types of XML parsers, viz, SAX and DOM . These parsers and their supporting classes are collectively called JAXP , Java API for XML Processing. Now given a XML document, how do I transform it in a user presentable format rather than parsing the XML document? Or do I need to parse the XML to transform it? No and may be. No - you don't have to parse it to transform it; java will do it for you with the help of XSL . May be - if you want to. Enough of theory. Let's program. Here is the sample XML file. <?xml version="1.0" encoding="UTF-8"?&g