JAXB - unmarshalled fields are null

jaxb unmarshal returns null values for elements
jaxb list of elements
unmarshalling soap response to java object example
marshalling and unmarshalling in java
jaxb unmarshal list of objects
how to unmarshal complex xml using jaxb
unmarshal soap message (xml) to java object using jaxb
package-info java jaxb

We are unmarshalling a response from http://xmlgw.companieshouse.gov.uk/. This is the text sent to the marshall:

<NameSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NameSearch.xsd">
    <CompanyName>COMPANY NAME</CompanyName>
  // more CoSearchItem elements

The model of CoSearchItem is like this:

@XmlType(name = "CoSearchItem", propOrder = {
public class CoSearchItem {

    @XmlElement(name = "CompanyName", required = true)
    protected String companyName;
    @XmlElement(name = "CompanyNumber", required = true)
    protected String companyNumber;
    @XmlElement(name = "DataSet", required = true)
    protected String dataSet;
    @XmlElement(name = "CompanyIndexStatus")
    protected String companyIndexStatus;
    @XmlElement(name = "CompanyDate")
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar companyDate;
    @XmlElement(name = "SearchMatch")
    protected String searchMatch;

    // getters and setters


NameSearch model has this structure:

@XmlType(name = "NameSearch", namespace = "http://xmlgw.companieshouse.gov.uk/v1-0/schema", propOrder = {
@XmlRootElement(name = "NameSearch", namespace = "http://xmlgw.companieshouse.gov.uk/v1-0/schema")
public class NameSearch {

    @XmlElement(name = "ContinuationKey", required = true)
    protected String continuationKey;
    @XmlElement(name = "RegressionKey", required = true)
    protected String regressionKey;
    @XmlElement(name = "SearchRows", required = true)
    protected BigInteger searchRows;
    @XmlElement(name = "CoSearchItem")
    protected List<CoSearchItem> coSearchItem;

    // setters and getters


The package has this annotations:

@XmlSchema(namespace = "http://xmlgw.companieshouse.gov.uk/v1-0", elementFormDefault = XmlNsForm.QUALIFIED, //
    xmlns = {
        @XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance") 

package uk.gov.companieshouse;

The unmarshaling is done from the first Node extracted from a larger Document, inside an any list of items. When we parse the xml however all the fields in CoSearchItem are set to null and can't figure out the reason.

Unmarshalling xml contents with own namespace results in null , For example, unmarshalling the xml into the java class below, you can Unmarshalling xml contents with own namespace results in null fields. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I figured out the correct answer thanks to @Blaise Doughan. After looking at the package namespace qualification I found that it was pointing to:


and it should have been pointing to:


Not sure how that got misplaced.

PI50020: JAXB UNMDARSHALLING DOES NOT WORK , Unmarshalling a simple XML string, all values will return null. When a ValidationEventHandler is set, the XML string is unmarshalled correctly. Here though, fields omitted (i.e. "note") are properly unmarshalled as null, but fields explicitly set to null (i.e. "firstName") are unmarshalled as the String value "null".

I solved this by making elementFormDefault="unqualified" in the xsd before generating stubs, else make the change manually in package-info.java

JAXB - Representing Null and Empty Collections, For the unmarshal use case the value of the nullCollection and emptyCollection the value of the fields will be whatever the class initialized them  This has potential to cause an attribute which is XML-mapped to a class using JAXB, to not get unmarshalled when the attribute is declared in a parent class. This therefore, can result in a null value being returned instead of an expected value.

JAXB, I have created a model class “ Employee ” which has some common fields. I want to build code which could parse a private List<Employee> employees = null ; Unmarshalling is the process to convert xml back to java object. Let's see the  JAXB Unmarshall Created an Empty Object (3) I am using JAXB to unmarshal an XML file into an object tree. The root object is non-null, but all of it's members are null even though there is data in the XML file. My object classes were generated with Axis2 from wsdls.

Unmarshaller (Java Platform SE 8 ), When the JAXBContext object does not have a mapping for the root element's name nor Since unmarshalling invalid XML content is defined in JAXB 2.0, the​  when I tried to unmarshal xml contents with its own namespace, every field in a tag with the namespace is null. But, with jdk version 1.8.0_101 it works without any trouble. For example, unmarshalling the xml into the java class below, you can see the id value is "foo" with jdk version 1.8.0_101 but is null with 1.8.0_152.

Unmarshaller (Java EE 6 ), When the JAXBContext object does not have a mapping for the root element's name nor Since unmarshalling invalid XML content is defined in JAXB 2.0, the​  target - non-null instance of JAXB mapped class prior to unmarshalling into it. parent - instance of JAXB mapped class that will eventually reference target . null when target is root element.

  • does this help?
  • It points to the @XmlSchema like Blaise did, so sort of. I found out the issue after realizing the incorrect namespace qualification.
  • Thanks for pointing me to the package schema annotation :) It turns out it wasn't the correct one.
  • There was a change in java around 1.8.0_100 that make this answer the only correct way to set schema for all fields. Before that version @XmlRootElement was also working.
  • That would do it. What was happening was that JAXB was thinking that each of the @XmlElement annotations was in the wrong namespace, so they weren't matching up when deserializing and so the data was getting dropped on the floor. (JAXB ignores unknown elements by default.)