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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:55:36+00:00 2026-05-26T18:55:36+00:00

I have a question about JAXB. Basically what I have are these two classes:

  • 0

I have a question about JAXB. Basically what I have are these two classes:

Element {
  String name
  List<Attribute> attributes;
}

Attribute {
  String key
  String value
}

Of course with getters and setters, and with JAXB XmlRootElement.

The XML generated from this is:

<element>
    <attributes>
        <key>id</key>
        <value>1</value>
    </attributes>
    <name>My Element</name>
</element>

But what I’m looking for is something more like this:

<element id="1">
    <name>My Element</name>
</element>

That is, for each instance of Attribute, I want key=value (as an attribute)

Is this possible in JAXB?

Regards,
Morten

  • 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-26T18:55:36+00:00Added an answer on May 26, 2026 at 6:55 pm

    I do not think that it will work with a List. But there is an alternative using a

    • Map and
    • @XmlAnyAttribute

    Your example:

      @XmlRootElement
      public static class Element
      {
        @XmlElement
        String             name;
    
        @XmlAnyAttribute
        Map<QName, Object> map;
      }
    
      {
        //
        Element element = new Element();
        element.name = "a wonderful name";
        element.map = new HashMap<QName, Object>();
        element.map.put( new QName( "id" ), "1" );
        element.map.put( new QName( "other" ), "2" );
      }
    

    Result of that:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <element id="1" other="2">
        <name>a wonderful name</name>
    </element>
    

    Best regards!

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

Sidebar

Related Questions

i have question about two different Bundle object in below methods : onSaveInstanceState(Bundle outState);
I have question about global variable in Objective-C. I have two .m files :
I'm learning asp.net. I have question about example buttons I can use two types
I have question about removing element from QList. myclass.h: class node2D : public QObject
Have a question about sending data between classes and activities. I currently have a
I have a question about the C++ visibility attribute. I have read http://gcc.gnu.org/wiki/Visibility and
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex

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.