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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:14:04+00:00 2026-06-08T00:14:04+00:00

An instance of my JAXB Object model contains an attribute that I want output

  • 0

An instance of my JAXB Object model contains an attribute that I want output when I generate Xml for the instance but not when I generate json

i.e I want

<release-group type="Album">
<title>Fred</title>
</release-group>

and

"release-group" : {
         "title" : "fred",
       },

but have

"release-group" : {
         "type" : "Album",
         "title" : "fred"
      },         

Can I do this using the oxml.xml mapping file

  • 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-08T00:14:05+00:00Added an answer on June 8, 2026 at 12:14 am

    Since your JSON binding is slightly different from your XML binding I would use EclipseLink JAXB (MOXy)‘s external mapping file.

    oxm.xml

    In the external mapping file we will mark the type field as transient.

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum383861">
        <java-types>
            <java-type name="ReleaseGroup">
                <java-attributes>
                    <xml-transient java-attribute="type"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    ReleaseGroup

    Below is the domain model I’ll use for this example. Note how the type property is annotated with @XmlAttribute.

    package forum383861;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="release-group")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ReleaseGroup {
    
        @XmlAttribute
        String type;
    
        String title;
    
    }
    

    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

    Since the XML and JSON representations are different we’ll create separate JAXBContexts for them. For the JSON one we’ll leverage MOXy’s external mapping file.

    package forum383861;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            ReleaseGroup rg = new ReleaseGroup();
            rg.type = "Album";
            rg.title = "Fred";
    
            // XML
            JAXBContext xmlJC = JAXBContext.newInstance(ReleaseGroup.class);
            Marshaller xmlMarshaller = xmlJC.createMarshaller();
            xmlMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            xmlMarshaller.marshal(rg, System.out);
    
            // JSON
            Map<String, Object> properties = new HashMap<String, Object>(2);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum383861/oxm.xml");
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {ReleaseGroup.class}, properties);
            Marshaller jsonMarshaller = jsonJC.createMarshaller();
            jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jsonMarshaller.marshal(rg, System.out);
        }
    
    }
    

    Output

    Below is the output from running the demo code:

    <?xml version="1.0" encoding="UTF-8"?>
    <release-group type="Album">
       <title>Fred</title>
    </release-group>
    {
       "release-group" : {
          "title" : "Fred"
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An instance of my JAXB Object model contains an element that I want output
I am using JAXB 2.0 JDK 6 in order to unmarshall an XML instance
I want to unmarshall the following XML structure using JAXB: <orCondition> <andCondition> <andCondition> <simpleCondition
Using JAXB (2) is it possible to ensure that null values are not marshalled
I'm trying to use JAXB to unmarshall some XML, but I'm getting an Unable
I need to unmarshall an XML that has namespaces for the attributes, for instance
For instance, (/=[^>]/, '═') I'd like to keep that match, but only replace the
I have a Java application that I want to save the data in XML
I have to generate a xml element that can have as value any primitive
Am trying to marshall a class that contains a List of Interface type. JAXB

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.