How to convert Java Object into/from XML – with Castor & Spring Boot

In the tutorial How to convert Java Object into/from XML, JavaSampleApproach will introduce way to Marshalling & Unmarshalling by an open source XML binding framework Castor with SpringBoot project.

Related Posts:
1. Gson to convert Java Object to/from Json
2. Jackson to convert Java Object to/from Json String


I. Technologies for convert Java Object into/from XML tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Castor
– SpringBoot – 1.5.7.RELEASE

II. Overview
1. Project Structure

Java Object into from XML project structure

2. Step to do

– Create SpringBoot project
– Create simple Model
– Create XML Converter
– Configure CastorMarshaller & XmlConverter
– Create Mapping file
– Develop main class for checking Marshalling & Unmarshalling
– Run & Check Results

III. Practices
1. Create SpringBoot project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, add project info. Press Next, then Finish, a Spring Boot project will be created successfully.

Open pom.xml file, add needed dependencies: Spring Oxm, Castor & xerces

2. Create simple Model

– Create Customer model:

3. Create XML Converter

Create XML Converter for handling Marshaller and Unmarshaller

4. Configure CastorMarshaller & XmlConverter

Because CastorMarshaller implements both Marshaller & Unmarshaller, so CastorMarshaller bean can use for XMLConverter as Marshaller & Unmarshaller:
– public class CastorMarshaller extends AbstractMarshaller
– public abstract class AbstractMarshaller implements Marshaller, Unmarshaller

Details code:

5. Create Mapping file

Mapping file is a way to control what field is element or attribute.

For locating the mapping file, use setMappingLocation method: castorMarshaller.setMappingLocation(appContext.getResource(“classpath:mapping.xml”));

6. Develop main class for checking Marshalling & Unmarshalling

In main class of SpringBoot:

7. Run & Check Results

Run the project with Spring Boot App mode.
Results:
1. Logs:

2. Content of customer.xml:

IV. Sourcecode

SpringBootMarshalling

By grokonez | January 19, 2017.

Last updated on November 6, 2017.



Related Posts


5 thoughts on “How to convert Java Object into/from XML – with Castor & Spring Boot”

    1. Hi,

      With Castor, We need a Mapping File to control the marshaling behavior. Up to now, We don’t see any support from annotations.

      Regards,

  1. Hi,

    I am getting ‘IllegalArgumentException’ while Marshalling, when Implementing the above solution. Can you help me resolve this?

    Stack Trace:
    java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.exolab.castor.mapping.loader.FieldHandlerImpl.getValue(FieldHandlerImpl.java:413) ~[castor-1.2.jar:na]
    at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1760) ~[castor-1.2.jar:na]
    at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:838) ~[castor-1.2.jar:na]

      1. I am getting the same error as mentioned. I have tried every possible solution in the last 14 hours, still clueless. Can you please help?

        Stacktrace :

        java.lang.IllegalArgumentException: object is not an instance of declaring class
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_181]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_181]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_181]
        at org.exolab.castor.mapping.loader.FieldHandlerImpl.getValue(FieldHandlerImpl.java:409) ~[castor-xml-1.3.3.jar:${VERSION}${RELEASE}]
        at org.exolab.castor.xml.Marshaller.processAttribute(Marshaller.java:2662) ~[castor-xml-1.3.3.jar:${VERSION}${RELEASE}]
        at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1534) ~[castor-xml-1.3.3.jar:${VERSION}${RELEASE}]
        at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:997) ~[castor-xml-1.3.3.jar:${VERSION}${RELEASE}]
        at com.demo.xml.Client.marshal(Client.java:328) ~[classes/:?]
        at com.demo.client.Processor.toString(Processor.java:244) ~[classes/:?]
        at com.demo.client.Processor$$FastClassBySpringCGLIB$$efa7e4a2.invoke() ~[classes/:?]
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at com.demo.client.Processor$$EnhancerBySpringCGLIB$$95910882.toString() ~[classes/:?]
        at com.demo.goal.test.SaveDemo.saveDemo(SaveDemo.java:71) ~[classes/:?]
        at com.demo.goal.test.DemoService.start(DemoService.java:138) ~[classes/:?]
        at com.demo.goal.test.DemoService$$FastClassBySpringCGLIB$$9bb23164.invoke() ~[classes/:?]
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at com.demo.goal.test.DemoService$$EnhancerBySpringCGLIB$$785cf808.start()

Got Something To Say:

Your email address will not be published. Required fields are marked *

*