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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:50:35+00:00 2026-06-01T12:50:35+00:00

There is a class called Parent which in generated from a xsd file and

  • 0

There is a class called Parent which in generated from a xsd file and holds a fully JABX annotated List. This class can’t be changed. Furthermore there is a Child extends Parent class that would like to use an XmlAdapter to convert the List to a HashMap. The HashMap field will have (could have) the same name and map to the same xsd element.

Can JAXB set a field with the same name multiple times? Does JAXB try to use the already filled List of the parent class as input for the XmlAdapter in the child class? I presume this won’t work the way I am hoping for.

How can this task be done neatly?

EDIT: My real question is really how to use JAXB autogenerated beans that shouldn’t be edited and still be able to use a hashmap.

Here’s my code which doesn’t work. The map stays null. Sry for the bad code formatting.

public class Adapter extends XmlAdapter<LinkedList<A>,HashMap<String,A>> {

    @Override
    public LinkedList<A> marshal(HashMap<String, A> v) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public HashMap<String, A> unmarshal(LinkedList<A> v) throws Exception {
        HashMap<String, A> map = new HashMap<String, A>();
        for(A a:v) {
            map.put(a.k, a);
        }
        return map;
    }
}

@XmlRootElement
public class Child extends Parent {

    @XmlElement(name="a")
    @XmlJavaTypeAdapter(Adapter.class)
    HashMap<String,A> map;
}

@XmlRootElement
public class Parent {

    @XmlElement(name="a")
    LinkedList<A> values = new LinkedList<A>();
}

public class XmlTest {

    public static void main(String[] args) throws ParserConfigurationException, JAXBException, SAXException, IOException {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new File("test.xml"));
        JAXBContext jc = JAXBContext.newInstance(Child.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Child instance = unmarshaller.unmarshal(doc,Child.class).getValue();
    }
}

Here is class A:

public class A {

    @XmlAttribute
    String v;

    @XmlAttribute
    String k;
}

Here the test.xml:

<root>
  <a v="1" k="a"/>
  <a v="2" k="b"/>
</root>
  • 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-01T12:50:37+00:00Added an answer on June 1, 2026 at 12:50 pm

    Have a look at example by Blaise Doughan. In your case class A is class MyMapEntryType, class Parent is class MyMapType. class Child should not extend Parent, as it is intermediate class just for JAXB to be able to map the list of <a> elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Path for which there are defined about 10 methods,
I have a model called Category which looks like this: class Category < ActiveRecord::Base
Let's say I have an abstract parent class called shape, and that there are
I have the following use case: There's a class called Template and with that
Is there a way for a class called inside another to know the name
I have a class called UserInfo that contains details about a given user. There
I have a ViewController class called GamePlay. In GamePlay there is a nested class
There's a feature called anonymous class in C++. It's similar with anonymous struct in
There is class Road with its Road.h and Road.cpp which contains implementation of method
I'm wondering if there is a better way to do this: class Foo {

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.