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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:23:40+00:00 2026-06-10T08:23:40+00:00

I have a requirement to marshal both json and xml outputs. But, how to

  • 0

I have a requirement to marshal both json and xml outputs. But, how to generate JSON output using MOXy JAXB in Spring? I can generate the xml file as shown in the example at: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Spring/JAXBDynamicOXM

The documentation does not have any examples for outputting json. I know that I can use JAXB Marshaller to generate the json output by setting the jaxb properties. How to do the same using MOXy/Spring/JAXB?

Appreciate your help!

  • 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-10T08:23:41+00:00Added an answer on June 10, 2026 at 8:23 am

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

    The following answer is based on the example from the MOXy wiki:

    • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Spring/JAXBDynamicOXM

    eclipselink-oxm.xml

    Normally JAXB/MOXy is used with a static domain model. In the example you lined to there there isn’t a real model, all the metadata is supplied via MOXy’s metadata file.

    <?xml version="1.0" encoding="US-ASCII"?>
    <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="example.gettingstarted">
        <java-types>
            <java-type name="Customer">
                <xml-root-element/>
                <java-attributes>
                    <xml-element java-attribute="name" type="java.lang.String" xml-path="personal-info/name/text()" />
                    <xml-element java-attribute="address" type="example.gettingstarted.Address" xml-path="contact-info/address" />
                    <xml-element java-attribute="phoneNumbers" type="example.gettingstarted.PhoneNumber" container-type="java.util.ArrayList" xml-path="contact-info/phone-number" />
                </java-attributes>
            </java-type>
            <java-type name="PhoneNumber">
                <java-attributes>
                    <xml-attribute java-attribute="type" type="java.lang.String"/>
                    <xml-value java-attribute="value" type="java.lang.String"/>
                </java-attributes>
            </java-type>
            <java-type name="Address">
                <xml-root-element/>
                <java-attributes>
                    <xml-element java-attribute="street" type="java.lang.String" xml-path="street/text()" />
                    <xml-element java-attribute="city" type="java.lang.String" xml-path="city/text()" />
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    jaxb.properties

    To use MOXy with dynamic models you need to specify a different jaxb.properties file than normal:

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory
    

    Demo

    In the example below we will convert an XML document into a dynamic model, and then marshal it to JSON. Note the context path (“forum12162216” in this example) must correspond to the package name that contains the jaxb.properties file. JSON binding is enabled by setting the "eclipselink.media-type" property on the Marshaller.

    package forum12162216;
    
    import java.io.File;
    import java.util.*;
    
    import javax.xml.bind.*;
    
    import org.eclipse.persistence.dynamic.DynamicEntity;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    import org.eclipse.persistence.jaxb.MarshallerProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum12162216/eclipselink-oxm.xml");
            JAXBContext jc = JAXBContext.newInstance("forum12162216", Demo.class.getClassLoader(), properties);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum12162216/customer.xml");
            DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
            marshaller.marshal(customer, System.out);
        }
    
    }
    

    customer.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <customer>
        <personal-info><name>Jane Doe</name></personal-info>
        <contact-info>
            <address>
              <city>My Town</city>
              <street>123 Any Street</street>
            </address>
            <phone-number type="work">613-555-1111</phone-number>
            <phone-number type="cell">613-555-2222</phone-number>
        </contact-info>
    </customer>
    

    Output

    {
       "customer" : {
          "personal-info" : {
             "name" : "Jane Doe"
          },
          "contact-info" : {
             "address" : {
                "street" : "123 Any Street",
                "city" : "My Town"
             },
             "phone-number" : [ {
                "type" : "work",
                "value" : "613-555-1111"
             }, {
                "type" : "cell",
                "value" : "613-555-2222"
             } ]
          }
       }
    }
    

    For More Information

    • http://blog.bdoughan.com/2011/08/binding-to-json-xml-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 requirement of specifying web part connections in onet.xml. So when site is
I have requirement to get Facebook friends list with their images. How can I
i have requirement in which opportunities of a particular record type can only updated
in iphone i have requirement of store image in cache . but how i
I have requirement to import Excel file in MySQL database using Java. I Googled
Hi I have requirement to create ,compile and load java classes run time. Using
In my project I have requirement to stream MP3 files from IIS using Smooth
we have requirement currently as shows in below image. have googled much but i
I have requirement of selecting s subset of data from a collection using a
I have requirement where as an attachment user can add a link from sharepoint

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.