Spring OXM Tutorial
Selected Reading
- Spring OXM - Discussion
- Spring OXM - Useful Resources
- Spring OXM - Quick Guide
- Spring OXM - Test Castor
- Spring OXM - Update Project
- Spring OXM - Test XStream
- Spring OXM - Update Project
- Spring OXM - Test JAXB2
- Spring OXM - Update Project JAXB2
- Spring OXM - Create Project
- Spring OXM - Environment Setup
- Spring OXM - Overview
- Spring OXM - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring OXM - Test Castor
Spring OXM - Test Castor
Update the main class OXMApppcation.java with marshaller and unmarshaller objects. The objective of this class is to marshall a student object to student.xml using marshaller object and then unmarshall the student.xml to student object using unmarshaller object.
Example
OXMApppcation.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.ApppcationContext; import org.springframework.context.support.ClassPathXmlApppcationContext; import org.springframework.oxm.Marshaller; import org.springframework.oxm.Unmarshaller; import org.springframework.oxm.XmlMappingException; import com.tutorialspoint.oxm.model.Student; pubpc class OXMApppcation { pubpc static void main(String[] args) { ApppcationContext context = new ClassPathXmlApppcationContext("apppcationcontext.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(); } } }
Output
Right cpck in the content area of the file in ecppse and then select Run as java apppcation and verify the output.
Oct 11, 2021 9:45:34 AM org.springframework.context.support.ClassPathXmlApppcationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApppcationContext@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 [apppcationcontext.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.seriapze.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: SoniyaAdvertisements