Menu

14 Eylül 2010 Salı

JAXB (Java Xml Binding)

JAXB (Java Xml Binding – https://jaxb.dev.java.net/) frameworkü kullanılarak Xml–>Java, Java–>Xml mapping işlemlerini gerçekleştirmek mümkündür. Bu yazılımcıya, Xml dosyalarının ihtiva ettiği verileri Java sınıflarıyla modelleme imkanı sunmakta ve verilerın işlenmesini kolaylaştırmaktadır.

Simdi daha once`den olusturmus oldugumuz schema1.xsd dosyasini kullanarak,  Xml–>Java mapping islemini yapacagiz... XJC kutuphanesinden yararlanarak yukaridaki bahsettigimiz islemleri gerceklestirmeye calisacagiz...



NOT : CLASSPATH ayarlarini oldugunu varsayarak yapilmistir, bundan onceki konumuzda, classpath icin gereken ayarlamalardan bahsedilmektedir....

JAXB tarafından oluşturulan sınıflar aşağıda yer almaktadır.
Student
package gensrc;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 *
Java class for student complex type.
 *
 *
The following schema fragment specifies the expected content contained within this class.
 *
 *
 * <complexType name="student">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="exams" type="{}exam" maxOccurs="unbounded" minOccurs="0"/>
 *         <element name="fullName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="id" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * 

 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "student", propOrder = {
    "exams",
    "fullName",
    "id"
})
public class Student {

    @XmlElement(nillable = true)
    protected List exams;
    protected String fullName;
    protected Long id;

    /**
     * Gets the value of the exams property.
     *
     *
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a set method for the exams property.
     *
     *

     * For example, to add a new item, do as follows:
     *

     *    getExams().add(newItem);
     * 

     *
     *
     *
     * Objects of the following type(s) are allowed in the list
     * {@link Exam }
     *
     *
     */
    public List getExams() {
        if (exams == null) {
            exams = new ArrayList();
        }
        return this.exams;
    }

    /**
     * Gets the value of the fullName property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getFullName() {
        return fullName;
    }

    /**
     * Sets the value of the fullName property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setFullName(String value) {
        this.fullName = value;
    }

    /**
     * Gets the value of the id property.
     *
     * @return
     *     possible object is
     *     {@link Long }
     *    
     */
    public Long getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     *
     * @param value
     *     allowed object is
     *     {@link Long }
     *    
     */
    public void setId(Long value) {
        this.id = value;
    }

}



Exam.java

package gensrc;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;


/**
 *
Java class for exam complex type.
 *
 *
The following schema fragment specifies the expected content contained within this class.
 *
 *

 * <complexType name="exam">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="examDate" type="{http://www.w3.org/2001/XMLSchema}dateTime" minOccurs="0"/>
 *         <element name="examScore" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * 

 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "exam", propOrder = {
    "examDate",
    "examScore",
    "name"
})
public class Exam {

    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar examDate;
    protected String examScore;
    protected String name;

    /**
     * Gets the value of the examDate property.
     *
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *    
     */
    public XMLGregorianCalendar getExamDate() {
        return examDate;
    }

    /**
     * Sets the value of the examDate property.
     *
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *    
     */
    public void setExamDate(XMLGregorianCalendar value) {
        this.examDate = value;
    }

    /**
     * Gets the value of the examScore property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getExamScore() {
        return examScore;
    }

    /**
     * Sets the value of the examScore property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setExamScore(String value) {
        this.examScore = value;
    }

    /**
     * Gets the value of the name property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setName(String value) {
        this.name = value;
    }

}


ObjectFactory

package gensrc;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each
 * Java content interface and Java element interface
 * generated in the gensrc package.
 * An ObjectFactory allows you to programatically
 * construct new instances of the Java representation
 * for XML content. The Java representation of XML
 * content can consist of schema derived interfaces
 * and classes representing the binding of schema
 * type definitions, element declarations and model
 * groups.  Factory methods for each of these are
 * provided in this class.
 *
 */
@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: gensrc
     *
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link Exam }
     *
     */
    public Exam createExam() {
        return new Exam();
    }

    /**
     * Create an instance of {@link Student }
     *
     */
    public Student createStudent() {
        return new Student();
    }
}


Simdide olusan class`larimi kullanarak, classlarimiza uygun XML data oluturalim 

import gensrc.Exam;
import gensrc.Student;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Main {
   
    public static void main(String[] args) throws Exception {

        Student student = new Student();
        student.setFullName("Ertugrul Aslan");
        student.setId(new Long(3123123));
        Exam e = new Exam();
        e.setExamDate(null);
        e.setExamScore("PASS");
        e.setName("Matematik");
        student.getExams().add(e);

        e = new Exam();
        e.setExamDate(null);
        e.setExamScore("PASS");
        e.setName("Fizik");
        student.getExams().add(e);

        Marshaller marshaller =  null;

        marshaller = JAXBContext.newInstance(Student.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(student, writer);

        System.out.println(writer.getBuffer().toString());

    }
}
 


Hiç yorum yok:

Yorum Gönder