Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:55:02+00:00 2026-06-11T15:55:02+00:00

I am receiving XML from a server whose schema specifies nearly every element as:

  • 0

I am receiving XML from a server whose schema specifies nearly every element as:

<xs:element name="myStringElementName" type="xs:string" nillable="true" minOccurs="0"/>
<xs:element name="myIntElementName" type="xs:int" nillable="true" minOccurs="0"/>

I’m trying to find a clean way to convert every element that I receive that is marked as xsi:nil="true" to a null when it is unmarshalled into a JAXB object. So something like this:

<myIntElementName xsi:nil="true" />

Should result in my JAXB object having a myIntElementName property with a value of null, rather than a JAXBElement object with a nil property set to true (or anything along those lines). I don’t have any control over the system that is sending me the XML that uses the nillable attribute, so I need to convert this on my end when I receive it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:55:03+00:00Added an answer on June 11, 2026 at 3:55 pm

    @XmlElement(nillable=true)

    You just need to specify @XmlElement(nillable=true) on your field/property to get this behaviour:

    @XmlElement(nillable=true)
    private String foo;
    

    Generating From an XML Schema

    Below I’ll demonstrate how to generate this mapping if you are staring from an XML schema.

    XML Schema (schema.xsd)

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="foo">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="myStringElementName" type="xs:string"
                        nillable="true" minOccurs="0" />
                    <xs:element name="myIntElementName" type="xs:int"
                        nillable="true" minOccurs="0" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    Why you get a property of type JAXBElement

    A property of type JAXBElement is generated in your model because you have a nillable element this is minOccurs="0". The use of JAXBElement allows the model to differentiate between an missing element (property is null) and the presence of the element with nil="true" (JAXBElement with nil flag set).

    <xs:element name="myStringElementName" type="xs:string"
                            nillable="true" minOccurs="0" />
    

    External Binding File (binding.xml)

    An external binding file can be specified to tell the JAXB implementation not to generate any properties of type JAXBElement. Note this will make it impossible for JAXB to round trip all XML documents.

    <?xml version="1.0" encoding="UTF-8"?>
    <jaxb:bindings version="2.0"
                   xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
        <jaxb:bindings>
            <jaxb:globalBindings generateElementProperty="false"/>
        </jaxb:bindings>
    </jaxb:bindings>
    

    XJC Call

    Below is an example of how to leverage an external binding file from the XJC Call

    xjc -b binding.xml schema.xsd
    

    Generated Model (Foo)

    The generated model will look something like the following:

    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "myStringElementName",
        "myIntElementName"
    })
    @XmlRootElement(name = "foo")
    public class Foo {
    
        @XmlElement(nillable = true)
        protected String myStringElementName;
        @XmlElement(nillable = true)
        protected Integer myIntElementName;
    
        public String getMyStringElementName() {
            return myStringElementName;
        }
    
        public void setMyStringElementName(String value) {
            this.myStringElementName = value;
        }
    
        public Integer getMyIntElementName() {
            return myIntElementName;
        }
    
        public void setMyIntElementName(Integer value) {
            this.myIntElementName = value;
        }
    
    }
    

    For More Information

    • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sending XML from C# Server and receiving it it in Android Java client This
We are receiving an XML file from our client. I want to load the
Problem: Flex/Flash4 client (built with FlashBuilder4) displays the xml sent from the server exactly
I'm receiving data from web-services in XML and I'm using that data via objects,
I am receiving an XML document from a REST service which shall be parsed
I am receiving XML data from a service. The test data I am receiving
I am currently receiving some compressed data from my server. I would like to
I am receiving an XML response from an API in a variable r so
I am working on a small project that is receiving XML data in string
I am receiving an xml feed which has values such as: <Theme>Valentine&#39;s Day</Theme> <Copyright>&#169;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.