I have an XSD which I have written like this:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gmt.com/provisioning/gtc/xml/Messaging" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="GTCMessage"> <xs:annotation> <xs:documentation> GTCMessage - To Pass Around using JMS. </xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:element name="type" type="xs:int" minOccurs="0"/> <xs:element name="scope" type="xs:int" minOccurs="0"/> <xs:element name="code" type="xs:int" minOccurs="0"/> <xs:element name="target" type="xs:string" minOccurs="0"/> <xs:element name="message" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Now I generate the JAXB classes using CXF maven plugin. And i get a JAXB class like (used a decompiler for this):
import com.gmt.provisioning.gtc.xml.messaging.GTCMessage; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.cxf.xjc.runtime.JAXBToStringStyle; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = {"type", "scope", "code", "target", "message"}) @XmlRootElement(name = "GTCMessage") public class GTCMessage { protected Integer type; protected Integer scope; protected Integer code; protected String target; protected String message; public Integer getType() { return this.type; } public void setType(Integer value) { this.type = value; } public boolean isSetType() { return (this.type != null); } public Integer getScope() { return this.scope; } public void setScope(Integer value) { this.scope = value; } public boolean isSetScope() { return (this.scope != null); } public Integer getCode() { return this.code; } public void setCode(Integer value) { this.code = value; } public boolean isSetCode() { return (this.code != null); } public String getTarget() { return this.target; } public void setTarget(String value) { this.target = value; } public boolean isSetTarget() { return (this.target != null); } public String getMessage() { return this.message; } public void setMessage(String value) { this.message = value; } public boolean isSetMessage() { return (this.message != null); } public String toString() { return ToStringBuilder.reflectionToString(this, JAXBToStringStyle.MULTI_LINE_STYLE); } }
Now to get it out, I wrote a simple class that just takes a string and unmarshalls it:
public class Test { public static void main (String args[]) { String abc = "<GTCMessage><type>1</type><scope>2</scope><code>1</code><message>16365343278450M</message></GTCMessage>"; GTCMessage aMessage = JAXB.unmarshal(new StringReader(abc), GTCMessage.class); System.out.println(aMessage.getMessage()); } }
But that last line prints null. I was expecting it to print the value 16365343278450M
. In fact every value in aMessage
object is null (scope, type etc).
I am suspecting that there might be something wrong with the XSD i wrote, that making it go wrong as domino effect.
Any pointers would be helpful. Thanks in advance.
Advertisement
Answer
I was able to fix it myself. There were two ways of doing it.
First was to change the string like:
String abc = "<GTCMessage xmlns="http://www.gmt.com/provisioning/gtc/xml/Messaging"><type>1</type><scope>2</scope><code>1</code><message>16365343278450M</message></GTCMessage>";
or change the XSD elementFormDefault
in the XSD and keep this original string without the namespace.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gmt.com/provisioning/gtc/xml/Messaging" elementFormDefault="unqualified" attributeFormDefault="unqualified">
I went for the latter as it was more manageable for me.