How To Converts Coffee Object To Xml - Jaxb Example

JAXB, stands for Java API for XML Binding or sometimes Java Architecture for XML Binding is a decade erstwhile technology scientific discipline to straight convert a Java object into XML document (marshaling) together with dorsum to XML file into Java object(unmarshalling). It uses combination of annotations together with getters together with setters to marshal Java object into XML. JAXB genuinely defines the demeanor of a measure prepare of tools together with interfaces that automatically generate Java flat files from XML schema, holler upwards JAXB is genuinely a framework together with architecture, non an implementation. The packet java.xml.bind provides a runtime binding framework for clients to marshal, unmarshal together with validate XML file inwards Java. BTW, JAXB is non the alone selection to parse XML inwards Java, you lot tin e'er move SAX or DOM parser or JAXP API to convert XML to Java object together with vice-versa. If you lot desire to larn to a greater extent than well-nigh XML processing inwards Java, I advise to await at chapter ii of Core Java Volume 2 By Cay S. Horstmann. This majority covers non alone DOM together with SAX exactly also StAX parser together with locating information amongst XPath.



How to move JAXB inwards Java - An Example

Let's encounter how you lot tin move JAXB to do XML document from Java classes This is how JAXB works, let's say you lot bring a Item object that needs to live on converted to XML document, all you lot postulate to do is do a flat Item amongst getters together with annotate them appropriately equally shown below :


@XmlRootElement public class Item{     private int id;     private String name;     private long price;          public Item(){         // no declaration constructor required past times JAXB     }     public Item(int id, String name, long price) {         super();         this.id = id;         this.name = name;         this.price = price;     }      @XmlElement     public int getId() {         return id;     }      @XmlElement     public String getName() {         return name;     }      @XmlElement     public long getPrice() {         return price;     }         }

Then you lot do a marshaller from JAXBContext flat together with inquire it to convert an illustration of flat Item into XML, equally shown below :

Item dvd = new Item(101, "Lord of the Rings", 10); JAXBContext context = JAXBContext.newInstance(Item.class); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(dvd, System.out);    

This volition impress next XML String into your console :

 stands for Java API for XML Binding or sometimes Java Architecture for XML Binding is a d How to converts Java Object to XML - JAXB Example

 
You tin encounter how slowly it is to convert a Java object to XML using JAXB. You don't postulate to worry well-nigh mapping types, opening tag, closing tag or doing anything related to XML past times hand.

 stands for Java API for XML Binding or sometimes Java Architecture for XML Binding is a d How to converts Java Object to XML - JAXB Example


Here is the consummate Java programme to convert a Java object to XML file using JAXB API :

import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement;  /** * Java programme to convert Java object to XML Document using * JAXB API. Converting a Java object to XML is also knonwn * equally marshalling together with contrary procedure is called unmarshalling. * * @author Javin Paul */ public class JAXBDemo{      public static void main(String args[]) throws JAXBException {          in conclusion Item dvd = new Item(101, "Lord of the Rings", 10);         in conclusion JAXBContext context = JAXBContext.newInstance(Item.class);         in conclusion Marshaller marshaller = context.createMarshaller();         marshaller.marshal(dvd, System.out);            }      }  @XmlRootElement class Item{     private int id;     private String name;     private long price;          public Item(){         // no declaration constructor required past times JAXB     }     public Item(int id, String name, long price) {         super();         this.id = id;         this.name = name;         this.price = price;     }      @XmlElement     public int getId() {         return id;     }      @XmlElement     public String getName() {         return name;     }      @XmlElement     public long getPrice() {         return price;     }           }



Things to croak along inwards take away heed :

1) Your Java flat must bring a no-argument constructor, otherwise JAXB volition non able to marshal it. You volition acquire next Exception :
Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Item does not bring a no-arg default constructor.     at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)     at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at javax.xml.bind.ContextFinder.newInstance(Unknown Source)     at javax.xml.bind.ContextFinder.newInstance(Unknown Source)     at javax.xml.bind.ContextFinder.find(Unknown Source)     at javax.xml.bind.JAXBContext.newInstance(Unknown Source)     at javax.xml.bind.JAXBContext.newInstance(Unknown Source)


2) Always holler upwards to annotate your Java flat amongst @XmlRootElement annotation, this would live on your principal tag inwards XML together with each belongings using @XmlElement, they volition appear equally fry chemical factor of the principal tag.



That's all well-nigh how to convert Java Object to XML. In side past times side tutorial you lot volition larn how to do unmarshalling i.e. converting an XML dorsum to Java object. Even though you lot powerfulness know well-nigh diverse XML parsers e.g. DOM, SAX together with StAX, its practiced to know JAXP together with JAXB equally well. As Java together with XML goes paw together with hand, they add together about other tool into Java developer's arsenal. If you lot are looking for about books to larn XML processing inwards Java, you lot tin e'er refer total Java majority ii for basic knowledge. It volition give you lot prissy overview of dissimilar parser, XML binding together with XPath, exactly if you lot desire to larn inwards to a greater extent than inwards depth thence you lot tin refer Java together with XML past times Brett McLaughlin. Its about other practiced majority to larn well-nigh XML processing inwards Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services together with REST API amongst Spring Boot
Java Web Fundamentals

Belum ada Komentar untuk "How To Converts Coffee Object To Xml - Jaxb Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel