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

  • Home
  • SEARCH
  • 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 6003517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:07:57+00:00 2026-05-23T01:07:57+00:00

I have a simple class that contains a hashmap: @XmlRootElement() public class Customer {

  • 0

I have a simple class that contains a hashmap:

@XmlRootElement()
public class Customer {

    private long id;
    private String name;

    private Map<String, String> attributes;

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }

    @XmlAttribute
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc =
           JAXBContext.newInstance("com.rbccm.dbos.payments.dao.test");

        Customer customer = new Customer();
        customer.setId(123);
        customer.setName("Jane Doe");

        HashMap<String, String> attributes = new HashMap<String, String>();
        attributes.put("a1", "v1");
        customer.setAttributes(attributes);


        StringWriter sw = new StringWriter();
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(customer, sw);
        System.out.println(sw.toString());

    }

}

The Main method produces the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:customer id="123" xmlns:ns2="http://www.example.org/package">
    <ns2:attributes>
        <entry>
            <key>a1</key>
            <value>v1</value>
        </entry>
    </ns2:attributes>
    <ns2:name>Jane Doe</ns2:name>
</ns2:customer>

The problem I have is that the namespace is dropped when outputting the hashmap. What I would like to generate is xml like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:customer id="123" xmlns:ns2="http://www.example.org/package">
    <ns2:attributes>
        <ns2:entry>
            <ns2:key>a1</ns2:key>
            <ns2:value>v1</ns2:value>
        </ns2:entry>
    </ns2:attributes>
    <ns2:name>Jane Doe</ns2:name>
</ns2:customer>
  • 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-23T01:07:58+00:00Added an answer on May 23, 2026 at 1:07 am

    You could use an XmlAdapter with you java.util.Map property to get the namespace qualification you are looking for.

    For an example of using XmlAdapter with java.uti.Map see:

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

    For more info on JAXB and namespaces :

    • http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html

    FYI

    I am considering adding an extension to EclipseLink JAXB (MOXy) to better handle this scenario:

    @XmlMap(wrapper="my-entry", key="@my-key", value="my-value")
    public Map<String, PhoneNumber> phoneNumbers = new HashMap<String, PhoneNumber>(); 
    

    The above annotation would correspond to the following XML:

    <phoneNumbers>
        <my-entry my-key="work">
            <my-value>613-555-1111</value>
        </my-entry>
    </phoneNumbers>
    

    The key/value properties would be XPath statements, and the namespace information would follow what is done for other MOXy extensions (for an example see link below):

    • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html

    Enhancement Request

    • https://bugs.eclipse.org/322423
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that contains public class PositionsChannelApplicationGroups { public PositionsChannelApplicationGroups(){} private Map<MyObj1,
I have a simple class that contains a string and an WPF Image control:
I have defined a class TestObject that contains two simple properties num and name.
I have a really simple Java class that effectively decorates a Map with input
I have a simple class that contains some general information about the current web
I have a simple POCO class that contains the student's scores. For example: Math
I have a simple class that contains the required properties for a request to
Assume you have the following simple objects: class Order { public Customer[] Customers {
I have a simple class that essentially just holds some values. I have overridden
I have a really simple class with two methods; One that will be called

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.