package com.jaxb;
import generated.Envelope;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class JAXBTest{
public static void main(String[] args) {
try {
InputStream stream = JAXBTest.class.getResourceAsStream("/test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Envelope.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Envelope envelope = (Envelope) jaxbUnmarshaller.unmarshal(stream);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
test.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Envelope version="01.00">
<Sender>
<Id>HRXMLEMPID</Id>
<Credential>25515</Credential>
</Sender>
<TimeStamp>4/11/2012 6:36:21 PM</TimeStamp>
<Manifest>HRB_PSID_CANDIDATE</Manifest>
<CANDIDATE>
<CANDIDATEID>12345</CANDIDATEID>
<!--Could be blank if there is no Req identifier sent from client -->
<REQUISITIONNUMBER/>
<BRREQNUMBER>123BR</BRREQNUMBER>
<JOBCODE>TestCode</JOBCODE>
<STATUS>Passed - Background Check</STATUS>
<FIRSTNAME>Rachel</FIRSTNAME>
<LASTNAME>Smith</LASTNAME>
<CONTACTMETHOD>Home</CONTACTMETHOD>
<!--Could be blank if there is no home phone no. for candidate. Format will be what end user has put in. -->
<HOMEPHONE>321/909-3003</HOMEPHONE>
<COUNTRY>United States</COUNTRY>
<!--Format will be what end user has put in. -->
<SSN>111-11-1111</SSN>
<!--1766 = HRB Seasonal Req Template; 1768 = HRB Corporate Req Template. Production values will be provided once the forms are created there.-->
<REQTEMPLATE>1766</REQTEMPLATE>
<!--Could be blank.-->
<PSIDCANDSTACKINGFIELD>12345</PSIDCANDSTACKINGFIELD>
</CANDIDATE>
</Envelope>
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Envelope" type="envelope"/>
<xsd:complexType name="envelope">
<xsd:sequence>
<xsd:element name="Sender" type="sender"/>
<xsd:element name="TimeStamp" type="xsd:string" />
<xsd:element name="Manifest" type="xsd:string" />
<xsd:element name="CANDIDATE" type="candidate"/>
</xsd:sequence>
<xsd:attribute name="version" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="sender">
<xsd:sequence>
<xsd:element name="Id" type="xsd:string" />
<xsd:element name="Credential" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="candidate">
<xsd:sequence>
<xsd:element name="CANDIDATEID" type="xsd:string" />
<xsd:element name="REQUISITIONNUMBER" type="xsd:string" />
<xsd:element name="BRREQNUMBER" type="xsd:string" />
<xsd:element name="JOBCODE" type="xsd:string" />
<xsd:element name="STATUS" type="xsd:string" />
<xsd:element name="FIRSTNAME" type="xsd:string" />
<xsd:element name="LASTNAME" type="xsd:string" />
<xsd:element name="CONTACTMETHOD" type="xsd:string" />
<xsd:element name="HOMEPHONE" type="xsd:string" />
<xsd:element name="COUNTRY" type="xsd:string" />
<xsd:element name="SSN" type="xsd:string" />
<xsd:element name="REQTEMPLATE" type="xsd:string" />
<xsd:element name="PSIDCANDSTACKINGFIELD" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Use this command to generate java bean out from the xsd
C:\Program Files\Java\jdk1.6.0_27\bin>xjc C:/jaxb/test.xsd
We need to manually add @XmlRootElement(name = "Envelope") in the Envelope class. No change required in any other beans
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2012.05.09 at 03:12:02 PM CDT
//
package generated;
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;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for envelope complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="envelope">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Sender" type="{}sender"/>
* <element name="TimeStamp" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Manifest" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="CANDIDATE" type="{}candidate"/>
* </sequence>
* <attribute name="version" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlRootElement(name = "Envelope")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "envelope", propOrder = {
"sender",
"timeStamp",
"manifest",
"candidate"
})
public class Envelope {
@XmlElement(name = "Sender", required = true)
protected Sender sender;
@XmlElement(name = "TimeStamp", required = true)
protected String timeStamp;
@XmlElement(name = "Manifest", required = true)
protected String manifest;
@XmlElement(name = "CANDIDATE", required = true)
protected Candidate candidate;
@XmlAttribute
protected String version;
/**
* Gets the value of the sender property.
*
* @return
* possible object is
* {@link Sender }
*
*/
public Sender getSender() {
return sender;
}
/**
* Sets the value of the sender property.
*
* @param value
* allowed object is
* {@link Sender }
*
*/
public void setSender(Sender value) {
this.sender = value;
}
/**
* Gets the value of the timeStamp property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTimeStamp() {
return timeStamp;
}
/**
* Sets the value of the timeStamp property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTimeStamp(String value) {
this.timeStamp = value;
}
/**
* Gets the value of the manifest property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getManifest() {
return manifest;
}
/**
* Sets the value of the manifest property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setManifest(String value) {
this.manifest = value;
}
/**
* Gets the value of the candidate property.
*
* @return
* possible object is
* {@link Candidate }
*
*/
public Candidate getCANDIDATE() {
return candidate;
}
/**
* Sets the value of the candidate property.
*
* @param value
* allowed object is
* {@link Candidate }
*
*/
public void setCANDIDATE(Candidate value) {
this.candidate = value;
}
/**
* Gets the value of the version property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getVersion() {
return version;
}
/**
* Sets the value of the version property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setVersion(String value) {
this.version = value;
}
}