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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:40:39+00:00 2026-05-31T01:40:39+00:00

I have been provided with an xsd that I compiled to Java classes using

  • 0

I have been provided with an xsd that I compiled to Java classes using JAXB.

The generated classes create an abstract class called “Event” and several classes that extend it.
e.g. “DerivedEvent”
I am using the following to marshal it to XML.

ObjectFactory objectFactory = new ObjectFactory();
DerivedEvent derivedEvent = objectFactory.createDerivedEvent();

JAXBContext context = JAXBContextImpl.newInstance("com.my.root.namespace");
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

Class partialClass = Event.class;
QName partNamespace = new QName(Event.class.getSimpleName());
Object element = new JAXBElement(partNamespace, partialClass, derivedEvent);
// Create a stringWriter to hold the XML
StringWriter stringWriter = new StringWriter();
marshaller.marshal(element, stringWriter);
String xml = stringWriter.toString(); 

This then outputs the wrong root element. i.e.

<Event xsi:type="DerivedEvent" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <DerivedStartPeriod xsi:nil="true"/>
    <DerivedEndPeriod xsi:nil="true"/>
</Event>

instead of

<DerivedEvent>
    <DerivedStartPeriod xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    <DerivedEndPeriod xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</DerivedEvent>

If I set the partialClass and partNamespace to DerivedEvent it outputs the correct information.
However, I can’t really do that as this marshalling is in a flow where the event could be one of 50 different derived events.

I can’t really change the xsd to have substitution groups as mentioned here http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html as this change is likely to be lost when I am provided with a new version of the xsd.

I need to use the partial class and namespace as the Events are not root elements.
Can I add the events to be XmlRootElements with bindings somehow?
Or is there another way to address this?

  • 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-31T01:40:40+00:00Added an answer on May 31, 2026 at 1:40 am

    I eventually found this page that helped me add the XmlRootElement:
    https://codereview.stackexchange.com/questions/1877/jaxb-xjc-code-generation-adding-xmlrootelement-and-joda-datetime

    Which I got working (dependencies/build.xml etc) by using the Annotate sample here:
    http://confluence.highsource.org/display/J2B/Home

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been provided with an unmanaged C++ DLL that contains several classes. I
I have always been using jaxb for parsing XML files into java object. This
I have been provided with a WSDL and have generated the proxy class based
I have been provided an XSD and a sample Xml file that contains the
I have tried all the solutions that have been provided including using PRAGMA but
I'm developing an iPad application. I have been provided with a PNG image that
I have been using the HorizontalListView sample provided at http://www.dev-smart.com/archives/34 . However my list
We have been provided with a wsdl and xsd schema by a company we
I know that similar questions have been asked before but the provided answers don't
We have been using a web application framework to build apps that need 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.