1

Hi I need to create following XML using JAXB but since it has many parent-child relationships , I don't want to make as many classes to create that XML. Anyone can give idea about how I can make this XML with the help of single class...

<Info>
    <details>
        <arrange>
            <name>joseph</name>
            <ID>12</ID>
            <Date>2012-03-25T11:23:42+10:00</Date>
            <LatestDate>
                <Start>2012-06-25T09:24:59+10:00</Start>
                <End>2013-06-25T09:24:59+10:00</End>
            </LatestDate>
            <Additional>
                <name>IVR</name>
            </Additional>
        </arrange>
    </details>
</Info>
bdoughan
  • 147,609
  • 23
  • 300
  • 400
Aquarius24
  • 1,806
  • 6
  • 33
  • 61

4 Answers4

2

Write an XSD and use JAXB's xjc code generator to create the classes.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
0

@XmlElementWrapper will do the job, you have tor write a single class and define every element with its wrapping element as you can read here: JAXB unmarshalling multiple XML elements into single class

Community
  • 1
  • 1
sschrass
  • 7,014
  • 6
  • 43
  • 62
0

You have to add the needed JAXB-Annotations to your class.

Then you will be able to parse a XML-File and get the Java-Objects.

Holger
  • 496
  • 3
  • 8
0

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

Since you are looking to map to the XML with a single class you can use MOXy's @XmlPath extension (see: http://blog.bdoughan.com/2010/07/xpath-based-mapping.html).

Info

import java.util.Calendar;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="Info")
@XmlAccessorType(XmlAccessType.FIELD)
public class Info {

    @XmlPath("details/arrange/name/text()")
    private String name;

    @XmlPath("details/arrange/ID/text()")
    private int id;

    @XmlPath("details/arrange/Date/text()")
    private Calendar date;

    @XmlPath("details/arrange/LatestDate/Start/text()")
    private Calendar start;

    @XmlPath("details/arrange/LatestDate/End/text()")
    private Calendar end;

    @XmlPath("details/arrange/Additional/name/text()")
    private String additionalName;

}

jaxb.properties

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Info.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum16956564/input.xml");
        Info info = (Info) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(info, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<Info>
   <details>
      <arrange>
         <name>joseph</name>
         <ID>12</ID>
         <Date>2012-03-25T11:23:42+10:00</Date>
         <LatestDate>
            <Start>2012-06-25T09:24:59+10:00</Start>
            <End>2013-06-25T09:24:59+10:00</End>
         </LatestDate>
         <Additional>
            <name>IVR</name>
         </Additional>
      </arrange>
   </details>
</Info>
bdoughan
  • 147,609
  • 23
  • 300
  • 400