Monday 28 January 2019

XML Marshalling and UnMarshalling using Spring and JAXB XML binding framework - Annotation Config


Click here to watch on Youtube:
https://www.youtube.com/watch?v=Qij5RqtLI_g&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge:
XML Marshalling and UnMarshalling using Spring and JAXB XML binding framework - Annotation Config

XML Marshalling and UnMarshalling using Spring and JAXB XML binding framework - Annotation Config

XML Marshalling and UnMarshalling using Spring and JAXB XML binding framework - Annotation Config

XML Marshalling and UnMarshalling using Spring and JAXB XML binding framework - Annotation Config

XML Marshalling and UnMarshalling using Spring and JAXB XML binding framework - Annotation Config

XML Marshalling and UnMarshalling using Spring and JAXB XML binding framework - Annotation Config

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ram.core</groupId>
    <artifactId>SpringDemo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringDemo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>5.1.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>

Company.java

package com.ram.core.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "CompanyInfo", namespace = "com.ram.core")
@XmlAccessorType(XmlAccessType.NONE)
public class Company
{
    @XmlAttribute(name = "Id")
    private Integer id;

    @XmlElement(name = "CompanyName")
    private String companyName;

    @XmlElement(name = "CEO_Name")
    private String ceoName;

    @XmlElement(name = "Number_Of_Employees")
    private Integer numberOfEmployees;

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getCompanyName()
    {
        return companyName;
    }

    public void setCompanyName(String companyName)
    {
        this.companyName = companyName;
    }

    public String getCeoName()
    {
        return ceoName;
    }

    public void setCeoName(String ceoName)
    {
        this.ceoName = ceoName;
    }

    public Integer getNumberOfEmployees()
    {
        return numberOfEmployees;
    }

    public void setNumberOfEmployees(Integer numberOfEmployees)
    {
        this.numberOfEmployees = numberOfEmployees;
    }

    @Override
    public String toString()
    {
        return "Company [id=" + id + ", companyName=" + companyName
                + ", ceoName=" + ceoName + ", numberOfEmployees="
                + numberOfEmployees + "]";
    }

}

XMLConverter.java

package com.ram.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

public class XMLConverter
{
    private Marshaller marshaller; 
    private Unmarshaller unmarshaller;

    public Marshaller getMarshaller()
    {
        return marshaller;
    }

    public void setMarshaller(Marshaller marshaller)
    {
        this.marshaller = marshaller;
    }

    public Unmarshaller getUnmarshaller()
    {
        return unmarshaller;
    }

    public void setUnmarshaller(Unmarshaller unmarshaller)
    {
        this.unmarshaller = unmarshaller;
    }

    public void convertFromObjectToXML(Object object, String filepath)
            throws IOException
    {

        FileOutputStream os = null;
        try
        {
            os = new FileOutputStream(filepath);
            getMarshaller().marshal(object, new StreamResult(os));
        }
        finally
        {
            if (os != null)
            {
                os.close();
            }
        }
    }

    public Object convertFromXMLToObject(String xmlfile)
            throws IOException
    {
        FileInputStream is = null;
        try
        {
            is = new FileInputStream(xmlfile);
            return getUnmarshaller().unmarshal(new StreamSource(is));
        }
        finally
        {
            if (is != null)
            {
                is.close();
            }
        }
    }

}

AppConfig.java

package com.ram.core.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

import com.ram.core.XMLConverter;

@Configuration
public class AppConfig
{
    @Bean
    public XMLConverter getHandler()
    {
        XMLConverter xmlConverter = new XMLConverter();
        xmlConverter.setMarshaller(getJaxbMarshaller());
        xmlConverter.setUnmarshaller(getJaxbMarshaller());
        return xmlConverter;
    }

    @Bean
    public Jaxb2Marshaller getJaxbMarshaller()
    {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setPackagesToScan("com.ram.core.model");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jaxb.formatted.output", true);
        jaxb2Marshaller.setMarshallerProperties(map);
        return jaxb2Marshaller;
    }

}

App.java

package com.ram.core;

import java.io.IOException;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.ram.core.config.AppConfig;
import com.ram.core.model.Company;

public class App
{
    private static final String XML_FILE_NAME = "company.xml";

    public static void main(String[] args) throws IOException
    {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
                AppConfig.class);
        XMLConverter converter = (XMLConverter) context
                .getBean(XMLConverter.class);

        Company company = new Company();
        company.setId(201);
        company.setCompanyName("Google");
        company.setCeoName("Peter");
        company.setNumberOfEmployees(50000);

        // from object to XML file
        converter.convertFromObjectToXML(company, XML_FILE_NAME);
        System.out.println("Converted Object to XML!");

        // from XML to object
        Company company2 = (Company) converter
                .convertFromXMLToObject(XML_FILE_NAME);
        System.out.println(company2);
        System.out.println("Converted XML to Object!");
       
    }
}

Output:

Converted Object to XML!
Company [id=201, companyName=Google, ceoName=Peter, numberOfEmployees=50000]
Converted XML to Object!

company.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:CompanyInfo xmlns:ns2="com.ram.core" Id="201">
    <CompanyName>Google</CompanyName>
    <CEO_Name>Peter</CEO_Name>
    <Number_Of_Employees>50000</Number_Of_Employees>
</ns2:CompanyInfo>

Click the below link to download the code:
https://sites.google.com/site/javaspringram2019/java_spring_2019/SpringDemo_JAXB_Annotation_Config.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java_Spring_2019/tree/master/Spring_2019/SpringDemo_JAXB_Annotation_Config

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/2db5cd1f4e7d5ab55ad7af886d6164700143c614/Spring_2019/SpringDemo_JAXB_Annotation_Config/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment