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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:45:52+00:00 2026-06-13T21:45:52+00:00

When I marshal java object using JAXB I am getting below xml element <error

  • 0

When I marshal java object using JAXB I am getting below xml element

<error line="12" column="" message="test" />

But I want xml as below

<error line="12" message="test" />

If column value is empty then I need to get the xml as shown above otherwise I need to get the column attribute in the element.

Is there any way to get 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-13T21:45:54+00:00Added an answer on June 13, 2026 at 9:45 pm

    An attribute will only be marshalled out with an empty String value if the corresponding field/property contains a empty String value. If the value is null the attribute will not be marshalled.

    Root

    package forum13218462;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        @XmlAttribute
        String attributeNull;
    
        @XmlAttribute
        String attributeEmpty;
    
        @XmlAttribute(required=true)
        String attributeNullRequired;
    
    }
    

    Demo

    package forum13218462;
    
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Root root = new Root();
            root.attributeNull = null;
            root.attributeEmpty = "";
            root.attributeNullRequired = null;
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root attributeEmpty=""/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Object that is being marshalled to XML using JAXB. One element
I'm getting this error when I use MarshallingView to marshal my list (java.util.List) of
I am using JAXB XMLadapter to marshal and unmarshal Boolean values. The application's XML
I am trying to unmarshall data from an XML object in Java using SimpleXML.
I am getting the below error when I try to use Xstream to marshal
I'm using Castor to marshal/unmarshal my Java objects, one of which contains an EnumMap.
Getting a 'marshal data too short' error when trying to install the mysql gem
I'm trying to marshal an object which has a DataHandler field with JAXB (2.1.12).
I have an application doing XML<->conversions using Jaxb and automatically generated classes with maven-jaxb2-plugin.
I'm new to JAXB and I want to marshal and un-marshal XML/Objects to Objects/XML.

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.