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

  • Home
  • SEARCH
  • 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 9001917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:30:02+00:00 2026-06-16T00:30:02+00:00

I am having trouble customizing my JAXB Marshaller. I have my marshaller code: public

  • 0

I am having trouble customizing my JAXB Marshaller. I have my marshaller code:

public void marshaller(AddressMap addMap, File file) {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(AddressMap.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(addMap, System.out);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

The output looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ObjectMap>
 <Prop> Indiana</Prop>
 <Prop1>39.0</Prop1>
 <Prop2>-85.0</Prop2>
 <Prop3> United States</Prop3>
 <Prop4> Hueseman Rd</Prop4>
 <Prop5> 8540-8704</Prop5>
 <Prop6> 47001</Prop6>
</ObjectMap>

Instead, I need it to look like this:

<bean class="classname">
    <property name="PropName" value="object value" />
    <property name="PropName1" value="object value" />
    <property name="PropName2" value="object value" />
    <property name="PropName3" value="object value" />
    <property name="PropName4" value="object value" />
    <property name="PropName5" value="object value" />
    <property name="PropName6" value="object value" />
</bean>
  • 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-16T00:30:02+00:00Added an answer on June 16, 2026 at 12:30 am

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    You could use MOXy’s @XmlDesciminatorNode and @XmlPath extensions to map this use case. Below is an example based on what I assume your object model looks like.

    ObjectMap

    The @XmlDescriminatorNode annotation allows you to specify that you want a specific XML attribute to serve as the inheritance indicator.

    package forum13884782;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;
    
    @XmlRootElement(name="bean")
    @XmlDiscriminatorNode("@class")
    public class ObjectMap {
    
    }
    

    AddressMap

    The @XmlDescriminatorValue annotation is used to specify the value on the descriminator node that relates to the instance class. In this class we also use the @XmlPath annotation to indicate which property element we wish to map to based on the value of its name attribute.

    package forum13884782;
    
    import javax.xml.bind.annotation.*;
    
    import org.eclipse.persistence.oxm.annotations.*;
    
    @XmlRootElement(name="bean")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlDiscriminatorValue("AddressMap")
    public class AddressMap extends ObjectMap {
    
        @XmlPath("property[@name='PropName']/@value")
        String prop;
    
        @XmlPath("property[@name='PropName1']/@value")
        String prop1;
    
        @XmlPath("property[@name='PropName2']/@value")
        String prop2;
    
        @XmlPath("property[@name='PropName3']/@value")
        String prop3;
    
        @XmlPath("property[@name='PropName4']/@value")
        String prop4;
    
        @XmlPath("property[@name='PropName5']/@value")
        String prop5;
    
        @XmlPath("property[@name='PropName6']/@value")
        String prop6;
    
    }
    

    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.

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

    Demo

    The following demo code will convert the XML message to an instance of AddressMap and then back to XML.

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

    input.xml/Output

    <bean class="AddressMap">
        <property name="PropName" value="Indiana" />
        <property name="PropName1" value="39.0" />
        <property name="PropName2" value="-85.0" />
        <property name="PropName3" value="United States" />
        <property name="PropName4" value="Hueseman Rd" />
        <property name="PropName5" value="8540-8704" />
        <property name="PropName6" value="47001" />
    </bean>
    

    For More Information

    • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.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

Having trouble with each function... Will try to explain by example... In my code,
Having trouble with grepping and cutting at the same time I have a file
Having trouble with an .htaccess file on WAMP. Works on the live server, but
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
Having trouble getting the following code to work. Calling swapLevel() directly works fine, but
Having trouble rewriting the name of a flash file: /flash/shell.1257347618.swf (the numbers are a
Having trouble with some select statements. I have 2 tables. sms_log and sms_messages. sms_log
Having trouble where I think I need to provide most of my code to

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.