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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:10:03+00:00 2026-05-17T00:10:03+00:00

I have written a JAXB mapping that stored a sublist inside a root element

  • 0

I have written a JAXB mapping that stored a sublist inside a root element in a LinkedHashMap<String, Object> instead of a Collection<Object> maintained through a specific XmlJavaTypeAdapter. Hereunder a sample:

@XmlRootElement
public class Parent {
    @XmlJavaTypeAdapter(ListToMapAdapter.class)
    @XmlElement
    private LinkedHashMap<String, Child> children;

    ...
}

The key is built from the attributes of the child tag (e.g. <child id="key"> give a Map.Entry<String, Child> like {key => child}.

One of my coworker says its bad designed, and that this Map should be in the object in charge of unmarshalling the XML. I’m not agree with him. Here are some pros and cons on such an approach. Which design do you think is the best? And which pros and cons did you see to complete the debate?

Goal of this design:

  • the Map give the ability to search efficiently children with a criteria (the key in my sample) instead of search by iteration on a Collecion.

Pros:

  • responsability for searching and filtering the sublist is at the nearest of the object and the parent object is responsible of retrieving and filtering its child,
  • the building of the Map is automatically made by JAXB when unmarshalling: elegant, efficient, and preserve from building the Map with a post process of the list after umarshalling (iteration),
  • the same object in different readers has still the ability of filtering its children,
  • subjective, it’s really a pretty way of doing that :),
  • if @XmlJavaTypeAdapter exists, it’s partly for this (numerous samples of this on the Web).

Cons (some are from my coworker):

  • JAXB limitation constraints to code to a concrete implementation instead of an interface: could not declare Map<String, Child> in my sample (not instantiable by JAXB while unmarshalling),
  • maybe (and its the argument of my coworker), its not the role of a mapping object (simple POJO according to him) to have such a behavior,
  • @XmlJavaTypeAdapter aims only at converting simple object to specific types: xs:date to Joda time Calendar for instance.

Thanks in advance for your 2 cents.

  • 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-17T00:10:04+00:00Added an answer on May 17, 2026 at 12:10 am

    I always recommend that people design the best model possible for their application. Then let JAXB handle the difficulty of mapping it to XML. In this case if it makes sense for parent to have a Map of children then by all means model it this way. JAXB does have some support for Map, but with the XML representation you describe you will need to use an XmlAdapter. For more information see:

    • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

    All of the Pros seem reasonable. None of the cons are valid:

    • As you can see from the above example when using @XmlJavaTypeAdaper your property is NOT restricted to a concrete implementation of Map.
    • The XmlAdapter is external to the POJO model, so the model does not have the list to/from map behaviour.
    • XmlAdapter is meant to be a catch all for any hard to map use case. It is not restricted to only simple objects. A common use case is for mapping instances of Map.

    If you want to eliminate the wrapper element, then you can use the @XmlPath extension in the MOXy JAXB implementation. The Foo class from my blog article would be modified slightly:

    import java.util.HashMap;
    import java.util.Map;
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Foo {
        @XmlJavaTypeAdapter(MyMapAdapter.class)
        @XmlPath(".")
        Map<Integer, String> map = new HashMap<Integer, String>();
    
        public Map<Integer, String> getMap() {
            return map;
        }
    
        public void setMap(Map<Integer, String> map) {
            this.map = map;
        }
    }
    

    For more information see:

    • http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
    • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several hand-written Java classes in a REST Server that uses JAXB to
I have written a code that draws grids and a triangle inside one grid
I have written some JavaScript that opens an element when an element is clicked.
have written this little class, which generates a UUID every time an object of
I have written this code inside a servlet to delete certain records from three
I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written a very simple CTE expression that retrieves a list of all
I have written a generic class class MyClass : MyClass<string> { } //this line
I have written the following code. I know that a higher order function is

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.