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 7093943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:30:08+00:00 2026-05-28T08:30:08+00:00

I have a String property in an object annotated as follows: @XmlElement(name = Item,

  • 0

I have a String property in an object annotated as follows:

@XmlElement(name = "Item", required = true, nillable = true)
private String item;

The result after marshaling is

<Item xsi:nil="true"/>

while I would like it to be

<Item/>

since the third-party service accepting my XML messages wants it like the latter case. I am using jaxb2. Does anyone knows how I could possibly do this?

Thanks a lot

  • 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-05-28T08:30:09+00:00Added an answer on May 28, 2026 at 8:30 am

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

    The following example requires the use of MOXy as the JAXB provider. This is because the JAXB RI does not call the XmlAdapter when the field/property is null. For information on specifying MOXy as your JAXB provider see:

    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

    StringAdapter

    The XmlAdapter will convert the String value to an object with a property annotated with @XmlValue.

    package forum8986842;
    
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class StringAdapter extends XmlAdapter<StringAdapter.AdaptedString, String>{
    
        @Override
        public String unmarshal(AdaptedString adaptedString) throws Exception {
            if(null == adaptedString) {
                return null;
            }
            String string = adaptedString.value;
            if("".equals(string)) {
                return null;
            }
            return string;
        }
    
        @Override
        public AdaptedString marshal(String string) throws Exception {
            AdaptedString adaptedString = new AdaptedString();
            adaptedString.value = string;
            return adaptedString;
        }
    
        public static class AdaptedString {
            @XmlValue public String value;
        }
    
    }
    

    Root

    The @XmlJavaTypeAdapter annotation is used to specify the XmlAdapter:

    package forum8986842;
    
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement(name="Root")
    public class Root {
    
        private String item;
    
        @XmlElement(name = "Item", required = true, nillable = true)
        @XmlJavaTypeAdapter(StringAdapter.class)
        public String getItem() {
            return item;
        }
    
        public void setItem(String item) {
            this.item = item;
        }
    
    }
    

    Demo

    The following code can be used to demonstrate the above mapping. Two documents are used one with an empty Item element, and the other with a populated Item element.

    package forum8986842;
    
    import java.io.StringReader;
    import javax.xml.bind.*;
    
    public class Demo {
    
        private JAXBContext jc;
    
        public Demo() throws JAXBException {
            jc = JAXBContext.newInstance(Root.class);
        }
    
        public static void main(String[] args) throws Exception {
            Demo demo = new Demo();
            demo.demo("<Root><Item/></Root>");
            demo.demo("<Root><Item>Hello World</Item></Root>");
        }
    
        private void demo(String xml) throws JAXBException {
            System.out.println("\n\nINPUT:  " + xml);
            StringReader stringReader = new StringReader(xml);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Root root = (Root) unmarshaller.unmarshal(stringReader);
    
            System.out.println("ITEM:   " + root.getItem());
    
            System.out.print("OUTPUT: ");
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    Output

    The following is the output from running the demo code:

    INPUT:  <Root><Item/></Root>
    ITEM:   null
    OUTPUT: <Root><Item/></Root>
    
    INPUT:  <Root><Item>Hello World</Item></Root>
    ITEM:   Hello World
    OUTPUT: <Root><Item>Hello World</Item></Root>
    

    For More Information

    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
    • http://blog.bdoughan.com/search/label/XmlAdapter
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain object and annotated as follows @Entity @Table(name = REQUEST) public
I have an object with a string[] property. When I assign this as the
I have persistent object, with a string property that often is over 500 charachters.
Hey. I have an object that has a string property called BackgroundColor. This string
I have a ViewModel which exposes the string property PageToolBarVisible which can be true
I have a domain object annotated like this for hibernate support. @Entity @Table(name =
Say I have a List of objects, and the object has a string property.
I have a property stored in a string... say Object Foo has a property
i have an object that has a string property called Status. I have a
I have several thousand objects with a string property in the format of yyyy-MM-ddTHH:mm:ssZ

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.