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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:43:53+00:00 2026-05-29T12:43:53+00:00

I’m trying to work with JAXB and @XmlJavaTypeAdaptor to map a Map during marshalling.

  • 0

I’m trying to work with JAXB and @XmlJavaTypeAdaptor to map a Map during marshalling. The only issue I’m having is that the “item” element that is used to contain each entry in the map is given a blank namespace.

Below is an example generated XML document that shows the issue:

<format xmlns="myNamespace">
    <data>
        <item xmlns:ns2="myNamespace" xmlns="">
            <ns2:key>Admin</ns2:key>
            <ns2:value>John</ns2:value>
        </item>
    </data>
</format>

I would like the fragment to look like this:

<format xmlns="myNamespace">
    <data>
        <item>
            <key>Admin</key>
            <value>John</value>
        </item>
    </data>
</format>

I’ve simplifed the real-life example to narrow it down to just this case. Here is the class that is being marshalled:

@XmlRootElement(name = "format")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public final class Format {

    @XmlTransient
    private final Map<String, String> data = new HashMap<String, String>();

    @XmlJavaTypeAdapter(MapAdapter.class)
    public Map<String, String> getData() {
        return data;
    }
}

Here is the implementation of XmlAdapter I am using:

public class MapAdapter extends XmlAdapter<MapElements[], Map<String, String>> {

    @Override
    public MapElements[] marshal(Map<String, String> stringStringMap) throws Exception {
        MapElements[] mapElements = new MapElements[stringStringMap.size()];
        int i = 0;

        for (Map.Entry<String, String> entry: stringStringMap.entrySet()) {
            mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());
        }

        return mapElements;
    }

    @Override
    public Map<String, String> unmarshal(MapElements[] mapElements) throws Exception {
        Map<String, String> map = new HashMap<String, String>();

        for (MapElements mapElement : mapElements) {
            map.put(mapElement.key, mapElement.value);
        }

        return map;
    }
}

I set the package level information like so, this does help in that it ensures the namespace for all elements except the “item” elements have the namespace “myNamespace”:

@XmlSchema(namespace = "myNamespace", elementFormDefault = XmlNsForm.QUALIFIED)
package org.prystasj;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

I’ve tried every combination of annotation, e.g. @XmlElement(namespace=”myNamespace”), I can think of throughout and there is no chance, so I think I’m missing something conceptual (or maybe even something rather trivial).

I’m using JAXB 2.2.2.

Thanks in advance for any insight.

  • 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-29T12:43:56+00:00Added an answer on May 29, 2026 at 12:43 pm

    You could use the following for your XmlAdapter

    import java.util.*;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class MapAdapter extends XmlAdapter<MapAdapter.MapElements, Map<String, String>> {
    
        @Override
        public MapElements marshal(Map<String, String> stringStringMap) throws Exception {
            MapElements mapElements = new MapElements();
            for (Map.Entry<String, String> entry: stringStringMap.entrySet()) {
                mapElements.item.add(new MapElement(entry.getKey(), entry.getValue()));
            }
            return mapElements;
        }
    
        @Override
        public Map<String, String> unmarshal(MapElements mapElements) throws Exception {
            Map<String, String> map = new HashMap<String, String>();
            for (MapElement mapElement : mapElements.item) {
                map.put(mapElement.key, mapElement.value);
            }
            return map;
        }
    
        public static class MapElements {
            public List<MapElement> item = new ArrayList<MapElement>();
        }
    
        public static class MapElement {
            public String key;
            public String value;
    
            public MapElement() {
            }
    
            public MapElement(String key, String value) {
                this.key = key;
                this.value = value;
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.