Saturday, June 04, 2011

Java Architecture for XML Binding (JAXB) Article

I was looking over the Java Architecture for XML Binding (JAXB) By Ed Ort and Bhakti Mehta from March 2003. It is a very good tutorial on JAXB, but it needed a little updating on some of the example code.

I am not sure how to get the code updated so, I will provide my modified version of UsingJAXBTest1. It uses generics, StringBuilder, and an enhanced for loop. I also removed some deprecated methods. Here is my new modified version with the original copyright.

UsingJAXBTest1


/**
 * $Id: UsingJAXBTest1.java,v 1.1 2003/01/01 03:18:32 bhakti Exp $
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sun.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
package test.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.List;

/**
 * This shows how to use JAXB to unmarshal an xml file
 * Then display the information from the content tree
 * @author bhakti
 * @author John Yeary
 */
public class UsingJAXBTest1 {

    public static void main(String args[]) {
        try {
            JAXBContext jc = JAXBContext.newInstance("test.jaxb");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Collection collection = (Collection) unmarshaller.unmarshal(new File("books.xml"));
            Collection.Books booksType = collection.getBooks();
            List<BookType> bookList = booksType.getBook();
            StringBuilder sb = new StringBuilder();

            for (BookType book : bookList) {
                sb.setLength(0);
                sb.append("Book  details\n");
                sb.append("Item id: ");
                sb.append(book.getItemId());
                sb.append("\n");
                sb.append("Book Name: ");
                sb.append(book.getName().trim());
                sb.append("\n");
                sb.append("Book ISBN: ");
                sb.append(book.getISBN());
                sb.append("\n");
                sb.append("Book Price: ");
                sb.append(book.getPrice().trim());
                sb.append("\n");
                sb.append("Book category: ");
                sb.append(book.getBookCategory());
                sb.append("\n");
                sb.append("Book promotion: ");
                sb.append(book.getPromotion().
                        getDiscount().trim());
                sb.append("\n");
                sb.append("No of Authors ");
                sb.append(book.getAuthors().getAuthorName().size());
                sb.append("\n");

                BookType.Authors authors = book.getAuthors();
                for (int j = 0; j < authors.getAuthorName().size(); j++) {
                    String authorName = authors.getAuthorName().get(j);
                    sb.append("Author Name ");
                    sb.append(authorName.trim());
                    sb.append("\n");
                }
                System.out.print(sb.toString());
            }

        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

0 comments :

Popular Posts