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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:16:54+00:00 2026-06-13T04:16:54+00:00

I’d like to marshall/unmarshall a Map into attributes of an XML element. I’ve seen

  • 0

I’d like to marshall/unmarshall a Map into attributes of an XML element. I’ve seen examples like:

<map>
<entry key="key1">value1</entry>
<entry key="key2">value2</entry>
</map>

What I really want is:

<map key1="value1" key2="value2"/>

Assume with me that there are no complex values and that they can legally be represented as XML attributes. Also, I’m trying to write this generically because the set of keys is not known until runtime.

How would I go about this? I’m familiar with XmlJavaTypeAdapter.

I thought about creating a MyMap that contains a List of entries but this wouldn’t get the output I’d like.

  • 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-13T04:16:56+00:00Added an answer on June 13, 2026 at 4:16 am

    Like I hinted in my comment, this cannot be achieved with JAXB alone. In the JAXB specification (JSR 222) it says:

    In all application scenarios, we create a Java object-level binding of the schema.

    That means that the scope of the binding is the same as the scope of the schema, which is static. A JAXB binding is not meant to be changed without recompiling the code. There are some exceptions, e.g. for xs:anyAttribute which is discussed in section 6.9 of the specification, but since you didn’t vote for the answer suggesting the use of @XmlAnyAttribute you probably don’t want to live with the limitations – e.g. only have QName keys in your map.

    I hope you are convinced that to do what you want with JAXB is a really bad idea, but just for reference below is an example that modifies the document after marshalling to bring it to the structure you want. You can copy and paste it into a single file and compile it with Java 7. The output will look like this:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <mapExample>
      <map France="Paris" Japan="Tokyo"/>
    </mapExample>
    

    My code only shows the marshalilng the other direction is equivalent:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMResult;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    @XmlRootElement
    class MapExample {
      @XmlJavaTypeAdapter(MapXmlAdapter.class)
      @XmlElement(name="map")
      private Map<String, String> data = new HashMap<>();
    
      public static void main(String[] args) throws Exception {
        MapExample example = new MapExample();
        example.data.put("France", "Paris");
        example.data.put("Japan", "Tokyo");
    
        JAXBContext context = JAXBContext.newInstance(MapExample.class);
        Marshaller marshaller = context.createMarshaller();
        DOMResult result = new DOMResult();
        marshaller.marshal(example, result);
    
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
    
        Document document = (Document)result.getNode();
        XPathExpression expression = xpath.compile("//map/entry");
        NodeList nodes = (NodeList)expression.evaluate(document, XPathConstants.NODESET);
    
        expression = xpath.compile("//map");
        Node oldMap = (Node)expression.evaluate(document, XPathConstants.NODE);    
        Element newMap = document.createElement("map");
    
        for (int index = 0; index < nodes.getLength(); index++) {
          Element element = (Element)nodes.item(index);
          newMap.setAttribute(element.getAttribute("key"), 
              element.getAttribute("value"));
        }
    
        expression = xpath.compile("//map/..");
        Node parent = (Node)expression.evaluate(document, XPathConstants.NODE);    
        parent.replaceChild(newMap, oldMap);
    
        TransformerFactory.newInstance().newTransformer().
          transform(new DOMSource(document), new StreamResult(System.out));
      }
    }
    
    class MapXmlAdapter extends XmlAdapter<MyMap, Map<String, String>> {
      @Override
      public Map<String, String> unmarshal(MyMap value) throws Exception {
        throw new UnsupportedOperationException();
      }
    
      @Override
      public MyMap marshal(Map<String, String> value) throws Exception {
        MyMap map = new MyMap();
        map.entries = new ArrayList<MyEntry>();
        for (String key : value.keySet()) {
          MyEntry entry = new MyEntry();
          entry.key = key;
          entry.value = value.get(key);
          map.entries.add(entry);
        }
        return map;
      }
    }
    
    class MyMap {
      @XmlElement(name="entry")
      public List<MyEntry> entries;
    }
    
    class MyEntry {
      @XmlAttribute
      public String key;
    
      @XmlAttribute
      public String value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.