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

  • Home
  • SEARCH
  • 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 6355053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:41:22+00:00 2026-05-24T22:41:22+00:00

Having a trivial code like this: @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class A { @XmlPath(B/text()) private String

  • 0

Having a trivial code like this:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class A {
  @XmlPath("B/text()")
  private String b;

  @XmlPath("C/text()")
  private Integer c;
  ...
}

It works absolutely fine as long as I have apt values in my XML. I’d like to mark the field c as required, so MOXy throw every time I try to read the document where c is either not set, or invalid. What’s the easiest solution?

Update:

Setting default values will also do.

  • 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-24T22:41:23+00:00Added an answer on May 24, 2026 at 10:41 pm

    EclipseLink JAXB (MOXy) or any JAXB implementation will not perform a set operation for missing nodes, so you could do the following:

    Option #1 – Default the field value

    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    class A {
      @XmlPath("B/text()")
      private String b = "fieldDefault";
    
      @XmlPath("C/text()")
      private Integer c;
    }
    

    With the following demo code:

    import java.io.StringReader;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(A.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            A a = (A) unmarshaller.unmarshal(new StringReader("<a/>"));
    
            System.out.println(a.getB());
            System.out.println(a.getC());
        }
    
    }
    

    Will produce the following output:

    fieldDefault
    null
    

    Option #2 – Specify defaultValue on @XmlElement

    You can specify a defaultValue on the @XmlElement annotation, but this will only set the defaultValue when an empty element is unmarshalled.

    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    class A {
      @XmlPath("B/text()")
      @XmlElement(defaultValue="annotationDefault")
      private String b;
    
      @XmlPath("C/text()")
      @XmlElement(defaultValue="annotationDefault")
      private Integer c;
    
    }
    

    With the following demo code:

    import java.io.StringReader;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(A.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            A a = (A) unmarshaller.unmarshal(new StringReader("<a><B/></a>"));
    
            System.out.println(a.getB());
            System.out.println(a.getC());
        }
    
    }
    

    Will produce the following output:

    annotationDefault
    null
    

    Option #3 – Specify an XML schema on the Unmarshaller to force validation

    Using MOXy or any JAXB implementation you can set an XML schema on the Unmarshaller to have the input validated:

    • http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some trivial code that looks like this: SortedDictionary<String, long> d = new
This may be something common and trivial, but I seem to be having trouble
This should be pretty trivial but for whatever reason, I'm having a little trouble
This is a trivial question, but for some reason I am having trouble with
Hey, I am guessing that this is probably fairly trivial, but I am having
This seems like a trivial problem, but my web searches for an answer (even
I feel like this is really basic, but I'm having trouble with this issue.
(Sorry if it's a trivial question.) I have documents that looks like this (Python
I thought this would be fairly trivial but I'm having problems... Using the Jquery
I am trying to use boost lambda to avoid having to write trivial functors.

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.