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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:58:42+00:00 2026-06-14T18:58:42+00:00

public class Person { public String name; … } When I marhsal I want

  • 0
public class Person {
    public String name; ...
}

When I marhsal I want to get a Name node with value attribute

<name value="arahant" />

instead of :

<name>arahant</name>

How can I achieve this? I tried looking at the XmlElementWrapper but that is allowed only for collections. Would I need to write custom code for this?

  • 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-14T18:58:43+00:00Added an answer on June 14, 2026 at 6:58 pm

    There are a couple of options available to you to support this use case.


    OPTION #1 – XmlAdapter ANY JAXB (JSR-222) IMPLEMENTATION

    This approach will work with any JAXB (JSR-222) compliant implementation.

    ValueAdapter

    An XmlAdapter allows you to marshal one object as if it were another object. In our XmlAdapter we will convert the String value to/from an object that has one property mapped with @XmlAttribute.

    package forum13489697;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class ValueAdapter extends XmlAdapter<ValueAdapter.Value, String>{
    
        public static class Value {
            @XmlAttribute
            public String value;
        }
    
        @Override
        public String unmarshal(Value value) throws Exception {
             return value.value;
        }
    
        @Override
        public Value marshal(String string) throws Exception {
            Value value = new Value();
            value.value = string;
            return value;
        }
    
    }
    

    Person

    The @XmlJavaTypeAdapter annotation is used to specify that the XmlAdapter should be used with a field or property.

    package forum13489697;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Person {
    
        @XmlJavaTypeAdapter(ValueAdapter.class)
        public String name;
    
    }
    

    OPTION #2 – EclipseLink JAXB (MOXy)

    I’m the EclipseLink JAXB (MOXy) lead and we offer the @XmlPath extension which allows you to easily do path based mapping.

    Person

    package forum13489697;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    public class Person {
    
        @XmlPath("name/@value")
        public String name;
    
    }
    

    jaxb.properties

    To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    DEMO CODE

    The following demo code can be used with either option:

    Demo

    package forum13489697;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Person.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum13489697/input.xml");
            Person person = (Person) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(person, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <person>
        <name value="arahant" />
    </person>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between: public class Person { public string Name { get;
Consider the following C# class: public class Person { public string Name {get;set} public
I have a model public class Person { public string Name { get; set;
I have the following Model: public class Person { public string Name {get;set;} public
I've got simple class public class Person { string Name { get; set; }
Example: public class person { public String Name {get;set;} } public static class FactoryStatic
I have this model : public class Person { public string Name { get;
I have the following data structure: public class Person { public string Name{get;set;} public
I have the following class: public class Person { public String Name { get;
Consider this simple example - public class Person { private String name; private Date

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.