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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:39:55+00:00 2026-06-18T05:39:55+00:00

I have a Object with two fields name and address. JAXB ignores the empty

  • 0

I have a Object with two fields “name” and “address”. JAXB ignores the empty elements while transforming the object into XMl.

For ex: if I have name=”xyz” and address=null then out will be

<name>xyz</name>

but what I want as an output as

<name>xyz</name>
<address></address>

I have seen the option @XmlElement(nillable="true") but this gives the output as

<name>xyz</name>
<address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

Please help me getting the desired output.

Thanks in advance.

  • 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-18T05:39:56+00:00Added an answer on June 18, 2026 at 5:39 am

    A JAXB (JSR-222) implementation will output an empty String "" value as an empty element. You can set the address property to this to get the desired effect.


    UPDATE #1

    I have updated my question. Basically the address element is NULL. Is
    this solution applicable to that as well?

    You could leverage Marshal Event Callbacks to adjust the value of address.

    import javax.xml.bind.Marshaller;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer {
    
        private String name;
        private String address;
    
        private void beforeMarshal(Marshaller marshaller) {
            if(null == address) {
                address = "";
            }
        }
    
        private void afterMarshal(Marshaller marshaller) {
            if("".equals(address)) {
                address = null;
            }
        }
    
    }
    

    UPDATE #2

    The only concern is that if I have 10 fields in the class I will have
    to write if for all the fields. Is there any other solution?

    If you use EclipseLink MOXy as your JAXB provider (I’m the MOXy lead), then you could use an XmlAdapter for this use case.

    XmlAdapter (StringAdapter)

    package forum14691333;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class StringAdapter extends XmlAdapter<String, String> {
    
        @Override
        public String marshal(String string) throws Exception {
            if(null == string) {
                return "";
            }
            return string;
        }
    
        @Override
        public String unmarshal(String string) throws Exception {
            if("".equals(string)) {
                return null;
            }
            return string;
        }
    
    }
    

    package-info

    Then if you specify it at the package level it will apply to all mapped fields/properties of type String within that package.

    @XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
    package forum14691333;
    
    import javax.xml.bind.annotation.adapters.*;
    

    For More Information

    • http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html
    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a Domain object Teacher with two fields String name, TeacherType teacherType,
I have a Person class with two properties: name and address . I want
I have one class Human , which contains two fields, age(int) , and name(String)
On the web side of things, I have two fields: name, and document. Document
I have two object : Mother and Child. Mother have many Children, How can
I have two tables: object that has object_id column and avalues that have object_id
Lets say I have an object with two boolean properties: public bool AdvancedMode{ get;
Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
Why two object's hash code is not same even though they have similar values.
I need to have some object hanging around between two events I'm interested in:

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.