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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:57:49+00:00 2026-05-19T13:57:49+00:00

I have a single java bean, which is already annotated for JPA, that I

  • 0

I have a single java bean, which is already annotated for JPA, that I also wish to store as XML, specifically FIXML. The goal is to manage the mapping from bean to XML with annotations.

I see related topics online about specifying a schema and letting JAXB generate classes, but I don’t want to do that.

I’ve been looking at using JAXB annotations, but it seems that I’ll need to make new classes for each child element. I’m trying to stay away from that, and let the annotations show how to construct the child elements. JAXB does not seem to want to do this.

Is this possible, and how? Do I need to make my own annotations and forget about JAXB?

Concrete example

Bean:

@Entity
@XmlRootElement(name="FIXML")
@XmlType(name="ExecRpt")
public class ExecutionReport implements Serializable {
    private String account;
    private String senderCompID;

    @Column(name="ACCOUNT", nullable=true, length=64)
    @XmlAttribute(name="Acct")
    public String getAccount() {
        return this.account;
    }
    public void setAccount(String account) {
        this.account = account;
    }

    @Column(name="SENDER_COMP_ID", nullable=true, length=200)
    @XmlAttribute(name="SID")
    public String getSenderCompID() {
        return this.senderCompID;
    }
    public void setSenderCompID(String senderCompID) {
        this.senderCompID = senderCompID;
    }
}

Parsing:

JAXBContext context = JAXBContext.newInstance(ExecutionReport.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
marshaller.marshal(executionReport, System.out);

Desired resulting XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FIXML>
  <ExecRpt Acct="account_data">
    <Hdr SID="sender"/>
  </ExecRpt>
</FIXML>

Current resulting XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FIXML Acct="account_data" SID="sender"/>

Clearly I’m not providing enough information to map the child elements yet, but I’m also not sure how to provide it. I want to add some @XmlElement annotations, but I don’t have child objects, all the data is in this class.

The upside is that my XML isn’t much more complicated than this example; there are only a handful of elements, and they only appear once per message. The thing that’s giving me trouble is getting multiple elements out of a single 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-05-19T13:57:49+00:00Added an answer on May 19, 2026 at 1:57 pm

    You can use the @XmlPath extension in EclipseLink JAXB (MOXy) for this, I’m the tech lead.

    Model Class

    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @Entity
    @XmlRootElement(name="FIXML")
    @XmlType(name="ExecRpt")
    public class ExecutionReport implements Serializable {
        private String account;
        private String senderCompID;
    
        @Column(name="ACCOUNT", nullable=true, length=64)
        @XmlPath("ExecRpt/@Acct")
        public String getAccount() {
            return this.account;
        }
        public void setAccount(String account) {
            this.account = account;
        }
    
        @Column(name="SENDER_COMP_ID", nullable=true, length=200)
        @XmlPath("ExecRpt/Hdr/@SID")
        public String getSenderCompID() {
            return this.senderCompID;
        }
        public void setSenderCompID(String senderCompID) {
            this.senderCompID = senderCompID;
        }
    }
    

    Demo Code

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception  {
            JAXBContext jc = JAXBContext.newInstance(ExecutionReport.class);
    
            ExecutionReport er = new ExecutionReport();
            er.setAccount("account_data");
            er.setSenderCompID("sender");
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(er, System.out);
        }
    
    }
    

    Resulting XML

    <?xml version="1.0" encoding="UTF-8"?>
    <FIXML>
       <ExecRpt Acct="account_data">
          <Hdr SID="sender"/>
       </ExecRpt>
    </FIXML>
    

    Specifying the EclipseLink JAXB (MOXy) Implementation

    To specify MOXy as the JAXB implementation you need to add a file called jaxb.properties in with your ExecutionReport class with the following entry:

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

    For More Information

    • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single user java program that I would like to have store
I learned today that NetBeans 6.5 should have an on-the-fly compilation of (single) Java
Is there a way to have a single implementation in Java that can read/parse
I have an single threaded, embedded application that allocates and deallocates lots and lots
I have a single line CEikLabel in my application that needs to scroll text.
I have a single spool mbox file that was created with evolution, containing a
I have a single entry output from a paradox table which is imported into
I have a single NSDictionary object which contains a large number of custom objects.
I have a single Rails 2.2.2 app that I want to 'share' with multiple
We have a Single Statement FUNCTION in SQL Server 2005 which uses CONTAINSTABLE(). All

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.