java corporate training - xml_jaxb1

Upload: praveen-kumar

Post on 14-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    1/9

    Siva Reddy

    CORE

    JAVA

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    2/9

    Class Summary

    XML

    What is XML

    Where to use XML

    XML vs HTML

    Defining XML XML naming rules

    Attributes

    DTD

    Java Corporate Training - Core Java 2

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    3/9

    Java Corporate Training - Core Java 3

    XML - JAXB Java Architecture for XML Binding (JAXB) is a Java standard that defines how Java

    objects are converted from and to XML.

    JAXB defines an API for reading and writing Java objects to and from XML

    documents.

    We can archive this with JAXP(Java API for XML Parser) with SAX and DOM. With

    JAXP programmer have to implement all the logic to create XML or read the XML

    and bind the element with objects. JAXB reduces this effort to parse or create XML

    with Java objects.

    JAXB uses different terminology to read and write the XML.

    Marshalling is a technique used to create XML from Java objects.

    UnMarshalling is a technique used to read XML and bind these data with

    Objects.

    JABX is API contains set of classes and interfaces to do marshalling andunmarshalling.

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    4/9

    Java Corporate Training - Core Java 4

    XML - JAXB

    This architecture works in two steps.

    In first step create a valid XSD and generate Java class files to

    store the XML data. Creating a class files are done by JAXB tool.

    Use IDE to do above task. Or you can do it my using XJC tool.

    xjc.sh -p -d Note you need to download the JAXB API and tools if your using JDK 1.4

    The second step is create objects by using generated classes and

    pass it to JAXB API.

    XML SchemaJava Classes &

    interfaces

    JAXB tool

    (Binding

    compiler)

    XML Document

    Application

    J

    avaClasses

    JAXB

    API

    Marshalling

    UnMarshalling

    Binding

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    5/9

    Java Corporate Training - Core Java 5

    XML - JAXB

    JAXB APIjavax.xml.bind.JAXBElement - Represents an element. XJC tool will genare

    ObJectFactory class. Use this class to generate the JAXB element.

    JAXBElement poElement = (new ObjectFactory()).create();

    javax.xml.bind.JAXBContext This is a base class for JAXB API. While creating an object

    we need to pass the Java classes path. Use below syntax.

    JAXBContext jaxbContext = JAXBContext.newInstance(");

    javax.xml.bind.Marshaller

    This class control the process of creating XML.

    Marshaller marshaller = jaxbContext.createMarshaller();

    To formate the data use below method.

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

    use below method to create XML.

    marshaller.marshal(, );

    javax.xml.bind.Unmarshaller - This class controls the process of unmarshalling.

    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    Use below method to map the data into java clases.

    unmarshaller.unmarshal();

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    6/9

    Java Corporate Training - Core Java 6

    XML - JAXB

    JAXB AnnotationsBelow are the main annotations used in JAXB API.

    @XmlType(propOrder = { "field2", "field1",.. }) - Allows to define the order in which thefields are written in the XML file.

    @XmlRootElement(namespace = "namespace") - Define the root element for a XML

    tree.

    @XmlElement(name = "neuName") - Define the XML element which will be used.

    Only need to be used if the neuNeu is different then the JavaBeans Name.

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    7/9

    Java Corporate Training - Core Java 7

    XML - JAXB

    Using Marshalling Create XML schema

    Generate Java Classes.

    Create Java objects with generated classes and pas the values. Use below code to generate XML.

    BankType bank=new BankType();

    Bank.setName(MyBank);

    JAXBElement poElement = (new ObjectFactory()).createBank(bank);

    JAXBContext jaxbContext = JAXBContext.newInstance("org.example.newxmlschema");

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(poElement , System.out);

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    8/9

    Java Corporate Training - Core Java 8

    XML - JAXB

    Using UnMarshalling Create XML schema

    Generate Java Classes.

    Use below code to read XML.

    JAXBContext jaxbContext = JAXBContext.newInstance("org.example.newxmlschema");

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    JAXBElement root=(JAXBElement)um.unmarshal(new

    File("sample.xml"));

    BankType bank=(BankType)root.getValue();

  • 7/29/2019 Java Corporate Training - XML_JAXB1

    9/9

    QUESTIONS?

    Java Corporate Training - Core Java 9