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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:33:43+00:00 2026-05-25T03:33:43+00:00

Here is what I have so far to marshall my POJO using JAXB :

  • 0

Here is what I have so far to marshall my POJO using JAXB :

@XmlRootElement
public class Local {
    private Entity entity;

    public void setEntity(Entity entity) {
        this.entity = entity;
    }

    @XmlElement
    public Entity getEntity() {
        return entity;
    }
}

and

@XmlRootElement
public class Entity {
    private String name;
    private String comment;

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

    @XmlAttribute
    public String getName(){
        return this.name;
    }

    public void setComment...

    @XmlAttribute
    public void getComment...
}

With that, I get something like this:

<local>
    <entity name="" comment=""></entity>
</local>

However, I would prefer to have the name attribute as an attribute of the local:

<local entityName="" entityComment=""></local>

Is the XmlJavaTypeAdapter a good way to begin with?

Thanks,
Alex

  • 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-25T03:33:43+00:00Added an answer on May 25, 2026 at 3:33 am

    There are a couple of different options to handle this use case:

    Option #1 – XmlAdapter (Any JAXB implementation)

    You could use an XmlAdapter for this use case. This will work as long as only one attribute value comes from the Entity object:

    EntityAdapter

    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class EntityAdapter extends XmlAdapter<String, Entity>{
    
        @Override
        public String marshal(Entity entity) throws Exception {
            if(null == entity) {
                return null;
            }
            return entity.getName();
        }
    
        @Override
        public Entity unmarshal(String name) throws Exception {
            Entity entity = new Entity();
            entity.setName(name);
            return entity;
        }
    
    }
    

    Local

    The XmlAdapter is linked with the field/property using the @XmlJavaTypeAdapter annotation:

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Local {
        private Entity entity;
    
        public void setEntity(Entity entity) {
            this.entity = entity;
        }
    
        @XmlAttribute
        @XmlJavaTypeAdapter(EntityAdapter.class)
        public Entity getEntity() {
            return entity;
        }
    
    }
    

    For More Information

    • http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
    • http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html

    Option #2 – @XmlPath (EclipseLink JAXB (MOXy)

    Alternatively if you are using EclipseLink JAXB (MOXy), the you could use the @XmlPath extension. This is useful with the Entity object corresponds to multiple XML attributes:

    Local

    Specifying the XPath “.” indicated that the child contents will be written into the parent element

    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.*;
    
    @XmlRootElement
    public class Local {
        private Entity entity;
    
        public void setEntity(Entity entity) {
            this.entity = entity;
        }
    
        @XmlPath(".")
        public Entity getEntity() {
            return entity;
        }
    }
    

    Entity

    public class Entity {
        private String name;
        private String comment;
    
        public void setName(String name){
            this.name = name;
        }
    
        @XmlAttribute(name="entityName")
        public String getName(){
            return this.name;
        }
    
        public void setComment(String comment){
            this.comment = comment;
        }
    
        @XmlAttribute(name="entityComment")
        public String getComment(){
            return this.comment;
        }
    }
    

    For More Information

    • http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
    • http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
    • http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Firefox Add-on Builder and here is what I have so far:
Here is what I have so far, it is not working: class Couple(o,t) one
Here is the code I have thus far: import java.io.*; class JAVAFilter implements FilenameFilter
Here is what I have so far : def mergesort[T <: Ordered[T]](elements : List[T])
http://pastebin.com/mYk8M038 here is what I have so far... I think it may be something
I'm a total newb to LINQ. Here is the code I have so far:
Here is what I have tried thus far: > cd /Applications/MAMP/bin/php5/bin/ > ./pear channel-discover
I really wonder why this question hasn't popped up here so far. I have
Here I have an arbitrary IEnumerable<T> . And I'd like to page it using
Here you have my switch from my Class Essaie with the Main() : switch(c)

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.