Saturday, February 25, 2012

JAXB (JAVA ARCHITECTURE FOR XML BINDING)

java objects are created to and from XML.the different annotations will be described in the program below.
1)Book.java(a class with getter setter methods)

XmlRootElement(name = "book")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = {"name", "id"})  //the order of output in XML.
public class Book {

    String name;
    int id;
    // If you like the variable name, e.g. "name", you can easily change this
    // name for your XML-Output:

    @XmlElement(name = "bookName")//will see the change when u see the XML as output
2)BookStore.java(a class with an arraylist and getter setter methods)

//This statement means that class "Bookstore.java" is the root-element of our example
@XmlRootElement(namespace = "jaxb implementation")//when u say namespace that means root element of XML.
public class BookStore {
// XmLElementWrapper generates a wrapper element around XML representation

    @XmlElementWrapper(name = "bookList")
    // XmlElement sets the name of the entities
    @XmlElement(name = "book")
    private ArrayList<Book> bookList;
    private String name;
    private String location;
3)BookMain.java the main class
set the values and include the book class object in the arraylist.also .
class x{
private static final String BOOKSTORE_XML = "./bookstore-jaxb.xml";
public static void main(){

// create JAXB context and instantiate marshaller
        JAXBContext context = JAXBContext.newInstance(BookStore.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(bookstore, System.out);

        Writer w = null;
        try {
            w = new FileWriter(BOOKSTORE_XML);
            m.marshal(bookstore, w);
        } finally {
            try {
                w.close();
            } catch (Exception e) {
            }
            // get variables from our xml file, created before
            System.out.println();
            System.out.println("Output from our XML File: ");
            Unmarshaller um = context.createUnmarshaller();
     
          BookStore bookstore2 =  (BookStore) um.unmarshal(new FileReader(
                    BOOKSTORE_XML));

           for (int i = 0; i < bookstore2.getBooksList().toArray().length; i++) {
                System.out.println("Book " + (i + 1) + ": "
                      +"id:"  + bookstore2.getBooksList().get(i).getId()+ " "
                     +"author"+":"+ bookstore2.getBooksList().get(i).getName()+"location"+":"+bookstore2.getLocation());
            }

        }
    }
output:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:bookStore xmlns:ns2="jaxb implementation">//see the rootelement
    <bookList>
        <book>
            <name>sougata</name>
            <bookName>6974</bookName>//see the name change"bookname"
        </book>
    </bookList>
    <location>Frankfurt Airport</location>
    <name>Fraport Bookstore</name>
</ns2:bookStore>

Output from our XML File:
Book 1: id:6974 author:sougata location:Frankfurt Airport

No comments:

Post a Comment