مشخصات مقاله
آموزش نگاشت در Java Spring با JAXB
Spring OXM
Spring با JAXB
مثال ادغام Spring و JAXB
JAXB مخفف معماری جاوا برای اتصال xml (Java Architecture for XML Binding) است و به توسعه دهندگان جاوا این امکان را می دهد تا کلاس جاوا را به فرم نمایش xml نگاشت کند. از JAXB می توان برای انتقال (راهنمایی) اشیای جاوا به XML و بالعکس استفاده کرد. این یک فریمورک OXM (نگاشت xml شی - Object XML Mapping) یا O/M ، ارائه شده توسط Sun است.
مزیت JAXB
نیازی به ساخت و استفاده از پارسر (parser) SAX یا DOM و نوشتن متدهای بازفراخوانی ندارید.
مثال ادغام Spring و JAXB (انتقال شی جاوا به xml)
برای انتقال شی جاوا به xml با استفاده از Spring با JAXB، فایل های زیر را باید ایجاد کنید.
- Employee.java
- applicationContext.xml
- Client.java
فایل های jar مورد نیاز :
برای اجرای این مثال فایل های jar هسته Spring و Web Spring را باید بارگذاری کنید.
Employee.java :
سه خصیصه شناسه، نام و حقوق را تعریف می کند. از نمادهای زیر در این کلاس استفاده شده است :
1- @XmlRootElement : عنصر ریشه را برای فایل xml مشخص می کند.
2- @XmlAttribute : ویژگی را برای خصیصه مشخص می کند.
3- @XmlElement : عنصر را مشخص می کند.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.javatpoint; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement (name= "employee" ) public class Employee { private int id; private String name; private float salary; @XmlAttribute (name= "id" ) public int getId() { return id; } public void setId( int id) { this .id = id; } @XmlElement (name= "name" ) public String getName() { return name; } public void setName(String name) { this .name = name; } @XmlElement (name= "salary" ) public float getSalary() { return salary; } public void setSalary( float salary) { this .salary = salary; } } <button></button> |
applicationContext.xml :
Bean،jaxbMarshallerBean را تعریف می کند، به طوری که کلاس Employee با فریمورک OXM محدود شده باشد.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | < ? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans < oxm:jaxb2-marshaller id = "jaxbMarshallerBean" > < oxm:class-to-be-bound name = "com.javatpoint.Employee" /> < / oxm:jaxb2-marshaller > < / beans > < button ></ button > |
Client.java :
از فایل applicationContext.xml یک نمونه از Marshaller می گیرد و متد Marshaller را فراخوانی می کند.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.javatpoint; import java.io.FileWriter; import java.io.IOException; import javax.xml.transform.stream.StreamResult; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.oxm.Marshaller; public class Client{ public static void main(String[] args) throws IOException{ ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); Marshaller marshaller = (Marshaller)context.getBean( "jaxbMarshallerBean" ); Employee employee= new Employee(); employee.setId( 101 ); employee.setName( "Sonoo Jaiswal" ); employee.setSalary( 100000 ); marshaller.marshal(employee, new StreamResult( new FileWriter( "employee.xml" ))); System.out.println( "XML Created Sucessfully" ); } } <button></button> |
خروجی مثال :
employee.xml
1 2 3 4 5 6 7 | < ? xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?> < employee id = "101" > < name >Sonoo Jaiswal< / name > < salary >100000.0< / salary > < / employee > < button ></ button > |