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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:11:44+00:00 2026-06-10T12:11:44+00:00

JAXB Object that contains ether a map or simple types The Idea is to

  • 0

JAXB Object that contains ether a map or simple types

The Idea is to have a class that has a member “value” which can contain simple types like String or Integer and also a Map of simple types, connected to a boolean value (eg. HashMap

Here is a “working” example, which shows what I want to do:

package test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;

@XmlRootElement
public class TestJAXB {
    public static void main(String[] args) {
        TestJAXB test = new TestJAXB();
        JAXB.marshal(test, new File("foo.xml"));
    }

    Container one;
    Container two;
    Container three;
    Container four;

    public TestJAXB() {
        this.one = new Container("foo");
        this.two = new Container("42");
        this.three = new Container(true);

        HashMap<Object, Boolean> map = new HashMap<Object, Boolean>();
        map.put("foo", false);
        map.put("bar", false);
        map.put("foobar", true);

        this.four = new Container(map);

    }

    @XmlElement
    public Container getOne() {
        return one;
    }

    public void setOne(Container one) {
        this.one = one;
    }

    @XmlElement
    public Container getTwo() {
        return two;
    }

    public void setTwo(Container two) {
        this.two = two;
    }

    @XmlElement
    public Container getThree() {
        return three;
    }

    public void setThree(Container three) {
        this.three = three;
    }

    @XmlElement
    public Container getFour() {
        return four;
    }

    public void setFour(Container four) {
        this.four = four;
    }
}

@XmlRootElement
@XmlSeeAlso(HashMap.class)
class Container {
    Map<Object, Boolean> value = null;
    boolean wasMap = false;

    protected Container() {

    }

    protected Container(Object value) {
        this.setValue(value);
    }

    @XmlElements({ @XmlElement(name = "string", type = String.class), @XmlElement(name = "bool", type = Boolean.class), @XmlElement(name = "int", type = Integer.class), })
    public Object getValue() {
        if (wasMap) {
            return value;
        } else {
            return value.keySet().toArray()[0];
        }
    }

    @SuppressWarnings("unchecked")
    public void setValue(Object value) {
        if (value instanceof Map) {
            this.value = (Map<Object, Boolean>) value;
            this.wasMap = true;
        } else {
            this.value = new HashMap<Object, Boolean>();
            this.value.put(value, false);
            this.wasMap = false;
        }
    }
}

Well, the simple types are marshalled to fine xml, but the element for the map stays empty:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testJAXB>
    <four>
        <string xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="hashMap"/>
    </four>
    <one>
        <string>foo</string>
    </one>
    <three>
        <bool>true</bool>
    </three>
    <two>
        <string>42</string>
    </two>
</testJAXB>

What did I do wrong? What can I 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-06-10T12:11:46+00:00Added an answer on June 10, 2026 at 12:11 pm

    You must create javax.xml.bind.annotation.adapters.XmlAdapter for HashMap.
    Example you can get here
    Example

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

Sidebar

Related Questions

I have an object graph that contains a cycle. How do I get JAXB
Assuming I have a schema that describes a root element class Root that contains
I am trying to marshal with jaxb an object which contains an Image, and
I am trying to marshal with jaxb an object which contains an Image, and
I have a JAX-RS web service (using jersey) that accepts a JAXB object as
How do I have JAXB preserve nulls when receiving a JSON sting that contains
I have an Object that is being marshalled to XML using JAXB. One element
An instance of my JAXB Object model contains an attribute that I want output
I am trying to marshal with jaxb an object which contains an Image, and
I am trying to marshal with jaxb an object which contains an Image, and

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.