- Spring OXM 教程
- Spring OXM - 主页
- Spring OXM - 概览
- Spring OXM - 环境设置
- Spring OXM & JAXB
- Spring OXM - 创建项目
- Spring OXM - 更新 Project 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 - 测试 XStream
使用 marshaller 和 unmarshaller 对象更新 main 类 OXMApplication.java。此类的目的是使用 marshaller 对象将学生对象编组到 student.xml 中,然后使用 unmarshaller 对象将 student.xml 解编为学生对象。
示例
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("xstreamMarshaller"); Unmarshaller unmarshaller = (Unmarshaller)context.getBean("xstreamMarshaller"); // 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:18:37 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@29ca901e: startup date [Mon Oct 11 09:18:36 IST 2021]; root of context hierarchy Oct 11, 2021 9:18:37 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 com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/intel/.m2/repository/com/thoughtworks/xstream/xstream/1.4.8/xstream-1.4.8.jar) to field java.util.TreeMap.comparator WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields 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
广告