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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:52:44+00:00 2026-05-16T18:52:44+00:00

There is a placeholder answer over at the unofficial guide with a link to

  • 0

There is a placeholder answer over at the unofficial guide with a link to an article which (to me) seems quite unrelated.

I use XJC to generate my JAXB classes and while most of them map to each other as expected, some elements get mapped to JAXBElement<Foo>. This is most annoying for graphs with cycles, where sometimes the parent node of a Foo element will be the JAXBElement<Foo>, which doesn’t itself have a parent property, breaking the cycle.

I can think of various workarounds, but it would be much nicer if someone could explain this behaviour to me. Why does JAXB sometimes map a <Foo> element to JAXBElement<Foo> instead of Foo?

  • 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-16T18:52:44+00:00Added an answer on May 16, 2026 at 6:52 pm

    JAXBElement is used to preserve the element name/namespace in use cases where enough information is not present in the object model. The most common occurence is with substitution groups:

    With Substitution Group:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org" 
        xmlns="http://www.example.org" 
        elementFormDefault="qualified">
    
        <xs:element name="root">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="anElement"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="anElement" type="xs:string"/>
    
        <xs:element name="aSubstituteElement" type="xs:string" substitutionGroup="anElement"/>
    
    </xs:schema>
    

    Will generate:

    package org.example;
    
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "anElement"
    })
    @XmlRootElement(name = "root")
    public class Root {
    
        @XmlElementRef(name = "anElement", namespace = "http://www.example.org", type = JAXBElement.class)
        protected JAXBElement<String> anElement;
    
        public JAXBElement<String> getAnElement() {
            return anElement;
        }
    
        public void setAnElement(JAXBElement<String> value) {
            this.anElement = ((JAXBElement<String> ) value);
        }
    
    }
    

    Without Substitution Group:

    If you remove the substitution group:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org" 
        xmlns="http://www.example.org" 
        elementFormDefault="qualified">
    
        <xs:element name="root">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="anElement"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="anElement" type="xs:string"/>
    
    </xs:schema>
    

    The following class will be generated:

    package org.example;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "anElement"
    })
    @XmlRootElement(name = "root")
    public class Root {
    
        @XmlElement(required = true)
        protected String anElement;
    
        public String getAnElement() {
            return anElement;
        }
    
        public void setAnElement(String value) {
            this.anElement = value;
        }
    
    }
    

    You may also get a JAXBElement when you unmarshal, compare the following examples:

    • Without any JAXB metada the result will be wrapped in a JAXBElement
    • Using @XmlRootElement eliminates the root level JAXBElement
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's placeholder on the page that is loaded asynchronously using jQuery load method. Page
Is there a way to dereference a placeholder inside lambda expression ? boost::function<int(MyClass*)> f
I have researched how to manually include a PlaceHolder Text in UITextView (as there
I am using jquery placeholder.js . Is there a possibility of hiding or removing
I am making an iPhone app which uses UIButton s with Placeholder text on
Is there any PHP PDF library that can replace placeholder variables in an existing
I need to count all X for which some_predicate(X) holds, and there really a
Instead of hardcoding text in the UI we use placeholders with keys which get
I have an aspx contentpage wherein there are two placeholders. <asp:PlaceHolder ID=plh1 runat=server Visible=false>
There is this SQL Statement SELECT t1.Name ,Count(t2.SubID) Totals -- I don't know how

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.