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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:52:10+00:00 2026-06-08T16:52:10+00:00

I need to marshall and unmarshall a Java class to XML. The class in

  • 0

I need to marshall and unmarshall a Java class to XML. The class in not owned by me, that I cannot add anotations so that I can use JAXB.

Is there a good way to convert the Java to XML with the given contraint?

Also, thought a tool may be helpful, but I would be more intersted it there is some Java API to do the same.

  • 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-08T16:52:11+00:00Added an answer on June 8, 2026 at 4:52 pm

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

    DOMAIN MODEL

    I will use the following domain model for this answer. Note how there are no JAXB annotations on the model.

    Customer

    package forum11693552;
    
    import java.util.*;
    
    public class Customer {
    
        private String firstName;
        private String lastName;
        private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public List<PhoneNumber> getPhoneNumbers() {
            return phoneNumbers;
        }
    
        public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
            this.phoneNumbers = phoneNumbers;
        }
    
    }
    

    PhoneNumber

    package forum11693552;
    
    public class PhoneNumber {
    
        private String type;
        private String number;
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
    }
    

    OPTION #1 – Any JAXB (JSR-222) Implementation

    JAXB is configurartion by exception, this means you only need to add annotations where you want the mapping behaviour to differ from the default. Below is a link to an example demonstrating how to use any JAXB impl without annotations:

    Demo

    package forum11693552;
    
    import javax.xml.bind.*;
    import javax.xml.namespace.QName;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Customer.class);
    
            Customer customer = new Customer();
            customer.setFirstName("Jane");
            customer.setLastName("Doe");
    
            PhoneNumber workPhone = new PhoneNumber();
            workPhone.setType("work");
            workPhone.setNumber("555-1111");
            customer.getPhoneNumbers().add(workPhone);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            JAXBElement<Customer> rootElement = new JAXBElement<Customer>(new QName("customer"), Customer.class, customer);
            marshaller.marshal(rootElement, System.out);
        }
    
    }
    

    Output

    <customer>
        <firstName>Jane</firstName>
        <lastName>Doe</lastName>
        <phoneNumbers>
            <number>555-1111</number>
            <type>work</type>
        </phoneNumbers>
    </customer>
    

    For More Information

    • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics

    OPTION #2 – EclipseLink JAXB (MOXy)’s External Mapping Document

    If you do want to customize the mappings, then you may be interested in MOXy’s external mapping document extension. A sample mapping document looks like the following:

    oxm.xml

    <?xml version="1.0"?>
    <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum11693552">
        <java-types>
            <java-type name="Customer">
                <xml-root-element />
                <java-attributes>
                    <xml-element java-attribute="firstName" name="first-name" />
                    <xml-element java-attribute="lastName" name="last-name" />
                    <xml-element java-attribute="phoneNumbers" name="phone-number" />
                </java-attributes>
            </java-type>
            <java-type name="PhoneNumber">
                <java-attributes>
                    <xml-attribute java-attribute="type" />
                    <xml-value java-attribute="number" />
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    jaxb.properties

    To enable 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

    When using EclipseLink MOXy as your JAXB provider (see), you can leverage the external mapping document when you bootstrap your JAXBContext

    package forum11693552;
    
    import java.util.*;
    import javax.xml.bind.*;
    import javax.xml.namespace.QName;
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String,Object>(1);
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum11693552/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
    
            Customer customer = new Customer();
            customer.setFirstName("Jane");
            customer.setLastName("Doe");
    
            PhoneNumber workPhone = new PhoneNumber();
            workPhone.setType("work");
            workPhone.setNumber("555-1111");
            customer.getPhoneNumbers().add(workPhone);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            JAXBElement<Customer> rootElement = new JAXBElement<Customer>(new QName("customer"), Customer.class, customer);
            marshaller.marshal(rootElement, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <customer>
       <first-name>Jane</first-name>
       <last-name>Doe</last-name>
       <phone-number type="work">555-1111</phone-number>
    </customer>
    

    For More Information

    • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
    • http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a recommendation for a pythonic library that can marshall python objects to
I am writing a Visual Studio add in and need to marshall a managed
I need to validate my JAXB objects before marshalling to an XML file. Prior
Can anyone give me a definitive answer as to whether I need to use
I need to Marshal a JAXB object to xml format string. I'm using a
I need to write a test which verifies that my code can handle an
Need help with a query that I wrote: I have three tables Company id
I need to use Jackson to marshal and unmarshal json on arbitrary objects, and
After going through Marshall code snippet Got the Idea that marshalling is used to
I have a string of XML that looks like this: <e1 atr1=3 atr2=asdf> <e1b

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.