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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:05:58+00:00 2026-05-23T06:05:58+00:00

BufferedWriter out = new BufferedWriter(fstream); try { JAXBContext context = JAXBContext.newInstance(NarociloType.class); Marshaller m =

  • 0
    BufferedWriter out = new BufferedWriter(fstream);
    try {
        JAXBContext context = JAXBContext.newInstance(NarociloType.class);

        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(parameters, out);
        out.newLine();
    } catch (PropertyException pe) {
        // TODO: Add catch code
        pe.printStackTrace();
    } catch (JAXBException jaxbe) {
        // TODO: Add catch code
        jaxbe.printStackTrace();
    }

but empty types are not stored into XML. For example:

NarociloType.date = null 

but i can not see in xml <date></date>.
JAXB marshalling does not create empty element for null values

Can i also change XML to object back with JAXBContext?

  • 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-23T06:05:59+00:00Added an answer on May 23, 2026 at 6:05 am

    Note: The following example works in EclipseLink JAXB (MOXy), but not the JAXB reference implementation included in Java SE 6.

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

    DateAdatper

    import java.util.Date;
    
    import javax.xml.bind.annotation.XmlValue;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    import forum235.DateAdapter.AdaptedDate;
    
    public class DateAdapter extends XmlAdapter<AdaptedDate, Date> {
    
        @Override
        public Date unmarshal(AdaptedDate adaptedDate) throws Exception {
            return adaptedDate.date;
        }
    
        @Override
        public AdaptedDate marshal(Date date) throws Exception {
            AdaptedDate adaptedDate = new AdaptedDate();
            adaptedDate.date = date;
            return adaptedDate;
        }
    
        static class AdaptedDate {
            @XmlValue
            public Date date;
        }
    
    }
    

    Root

    import java.util.Date;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Root {
    
        private Date date;
    
        @XmlJavaTypeAdapter(DateAdapter.class)
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
    }
    

    Demo

    import java.util.Date;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            Root root = new Root();
            marshaller.marshal(root, System.out);
    
            root.setDate(new Date());
            marshaller.marshal(root, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <date/>
    </root>
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <date>2011-06-16T09:16:09.452</date>
    </root>
    

    jaxb.properties

    You use MOXy as your JAXB provider you need to provide a file called jaxb.properties in the same package as your domain classes with the following entry:

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    For More Information on XmlAdapter

    • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
    • http://bdoughan.blogspot.com/2011/05/jaxb-and-joda-time-dates-and-times.html
    • http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
    • http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my code try { fstream = new FileWriter(/Applications/AirDrop/contacts.txt); BufferedWriter out = new
At the moment I have this: try{ // Create file FileWriter fstream = new
I am writing into a java file through FileWriter fstream = new FileWriter(someFile.java); BufferedWriter
BufferedWriter out = new BufferedWriter( new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream(out.txt) ) ) );
My ServerSocket writes out the following lines: OutputStreamWriter outstream = new OutputStreamWriter(clientSocket.getOutputStream()); BufferedWriter out
I tried this, but without sucess: File temp = File.createTempFile(document, .htm); BufferedWriter out =
I'm looking at the following example Which uses the following code try { BufferedWriter
Is it possible to somehow 'import' a new Java class into a running program
I tried a simple file write program in Android. try { //File f1=new File(/sdcard/Prag.txt);
In a Java program (Java 1.5), I have a BufferedWriter that wraps a Filewriter,

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.