Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping XML Entities to Java Objects

Tags:

I am quite sure, this is one of the many duplicated questions around XML to Java Object conversions. But I started this thread since I couldn't find simpler or looking for simpler solution.

I have an xsd [Infact I am designing it] and xml. I would like to auto-map the xml data to Java beans as per mapping

<tns:SummaryCart xmlns:tns="SummaryCart" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="SummaryCart.xsd">     <SummaryElement type="test">         <order>1</order>         <id>A</id>         <displayName>A</displayName>         <subElements>             <order>1</order>             <id>Preactivation</id>             <displayName>Preactivation</displayName>         </subElements>         <maxlines>1</maxlines>     </SummaryElement> </tns:SummaryCart> 

Now my Java classes will be

public class SummaryCart{     private List<SummaryElement> summaryElementList; } public class SummaryElement {     private int order;     private String id;     private String displayName;     private String property;     private List<SummaryElement> subElements;     private int maxlines;     private String type; } 

Is there any simple tool/framework which can auto-map the data from XML to Java beans [MUST support attributes/element mapping]. Tutorial will be good.

Btw, I am using Spring framework, if spring-oxm advantage is taken, its welcome.

like image 387
RaceBase Avatar asked Apr 08 '13 14:04

RaceBase


People also ask

How do you Unmarshal XML string to Java object using JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

Is XML compatible with Java?

XML provides a universal syntax for Java semantics (behavior). Simply put, this means that a developer can create descriptions for different types of data to make the data behave in various ways with Java programming code, and can later repeatedly use and modify those descriptions.

What is XML map Java?

Introduction. Castor XML mapping is a way to simplify the binding of java classes to XML document. It allows to transform the data contained in a java object model into/from an XML document.


2 Answers

Below is how you could map your object to XML using JAXB (JSR-222). An implementation is included in the JDK/JRE starting with Java SE 6. JAXB is supported by Spring (see section 8.5: http://static.springsource.org/spring-ws/site/reference/html/oxm.html)

SummaryCart

import java.util.List; import javax.xml.bind.annotation.*;  @XmlRootElement(name="SummaryCart", namespace="SummaryCart") @XmlAccessorType(XmlAccessType.FIELD) public class SummaryCart{      @XmlElement(name="SummaryElement")     private List<SummaryElement> summaryElementList;  } 

SummaryElement

import java.util.List; import javax.xml.bind.annotation.*;  @XmlAccessorType(XmlAccessType.FIELD) public class SummaryElement {      private int order;     private String id;     private String displayName;     private String property;     private List<SummaryElement> subElements;     private int maxlines;      @XmlAttribute     private String type;  } 

Demo

import java.io.File; import javax.xml.bind.*;  public class Demo {      public static void main(String[] args) throws Exception {         JAXBContext jc = JAXBContext.newInstance(SummaryCart.class);          Unmarshaller unmarshaller = jc.createUnmarshaller();         File xml = new File("src/forum15881876/input.xml");         SummaryCart sc = (SummaryCart) unmarshaller.unmarshal(xml);          Marshaller marshaller = jc.createMarshaller();         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);         marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "SummaryCart.xsd");         marshaller.marshal(sc, System.out);     }  } 

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:SummaryCart xmlns:ns2="SummaryCart" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="SummaryCart.xsd">     <SummaryElement type="test">         <order>1</order>         <id>A</id>         <displayName>A</displayName>         <subElements>             <order>1</order>             <id>Preactivation</id>             <displayName>Preactivation</displayName>             <maxlines>0</maxlines>         </subElements>         <maxlines>1</maxlines>     </SummaryElement> </ns2:SummaryCart> 
like image 73
bdoughan Avatar answered Oct 24 '22 23:10

bdoughan


Basically you want to unmarshal your XML. Here's a detailed tutorial that describes how to use the JAXB xjc command to generate a Java class from XML Schema. A maven xjc plugin is also available for your convenience.

like image 32
Jops Avatar answered Oct 25 '22 01:10

Jops