- Spring OXM 教程
- Spring OXM - 主頁
- Spring OXM - 概述
- Spring OXM - 環境設定
- Spring OXM 和 JAXB
- Spring OXM - 建立專案
- Spring OXM - 更新專案 JAXB2
- Spring OXM - 測試 JAXB2
- Spring OXM 和 XStream
- Spring OXM - 更新專案
- Spring OXM - 測試 XStream
- Spring OXM 和 Castor
- Spring OXM - 更新專案
- Spring OXM - Castor 測試
- Spring OXM 有用資源
- Spring OXM - 快速指南
- Spring OXM - 有用資源
- Spring OXM - 討論
Spring OXM - Castor 測試
使用 marshaller 和 unmarshaller 物件更新主類 **OXMApplication.java**。此類的目的是使用 marshaller 物件將 student 物件編組到 student.xml,然後使用 unmarshaller 物件將 student.xml 取消編組到 student 物件。
示例
OXMApplication.java
package com.tutorialspoint.oxm;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
import com.tutorialspoint.oxm.model.Student;
public class OXMApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
Marshaller marshaller = (Marshaller)context.getBean("castorMarshaller");
Unmarshaller unmarshaller = (Unmarshaller)context.getBean("castorMarshaller");
// create student object
Student student = new Student();
student.setAge(14);
student.setName("Soniya");
try {
marshaller.marshal(student, new StreamResult(new FileWriter("student.xml")));
System.out.println("Student marshalled successfully.");
FileInputStream is = new FileInputStream("student.xml");
Student student1 = (Student)unmarshaller.unmarshal(new StreamSource(is));
System.out.println("Age: " + student1.getAge() + ", Name: " + student1.getName());
} catch(IOException | XmlMappingException ex) {
ex.printStackTrace();
}
}
}
輸出
右鍵單擊 eclipse 中檔案的目標區域,然後選擇 **以 Java 應用程式執行**,並確認輸出。
Oct 11, 2021 9:45:34 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6adede5: startup date [Mon Oct 11 09:45:34 IST 2021]; root of context hierarchy Oct 11, 2021 9:45:35 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationcontext.xml] WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.exolab.castor.xml.BaseXercesOutputFormat (file:/C:/Users/intel/.m2/repository/org/codehaus/castor/castor-xml/1.4.1/castor-xml-1.4.1.jar) to method com.sun.org.apache.xml.internal.serialize.OutputFormat.setMethod(java.lang.String) WARNING: Please consider reporting this to the maintainers of org.exolab.castor.xml.BaseXercesOutputFormat WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations. WARNING: All illegal access operations will be denied in a future release Student marshalled successfully. Age: 14, Name: Soniya
廣告