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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:22:23+00:00 2026-05-28T07:22:23+00:00

I’m facing a marshalling/unmarshalling problem involving inheritance and polymorphism using MOXy’s JAXB implementation and

  • 0

I’m facing a marshalling/unmarshalling problem involving inheritance and polymorphism using MOXy’s JAXB implementation and external metadata bindings file.

I have no control on the XML files or the model classes.

There are multiple classes inside the model that inherit other DTO classes.
Here is an example of the environment I’m working in. This example is only here for some syntax purpose, the real environment involves nested inheritance, collections etc. :

Here is the class that will be inherited

  class A {

        private String name;

        public String getName(){
              return name;
        }

        public void setName(String value){
              name = value;
        }

  } 

Here is one inherited class

  class B extends A {

        private String attrFromB;

        public String getAttrFromB(){
              return attrFromB;
        }

        public void setAttrFromB(String value){
              attrFromB = value;
        }
  } 

And another

  class C extends A {

        private String attrFromC;

        public String getAttrFromC(){
              return attrFromC;
        }

        public void setAttrFromC(String value){
              attrFromC= value;
        }
  } 

Here is a container class

  class MyContainerClass{

        private A myObject;

        public A getMyObject(){
           return myObject;
        }

        public void setMyObject(A value){
           myObject = value;
        }
  }

Here is the XML that it should produce in the case of MyContainer containing A

  <MyContainer>
        <MyObject nameA="foo" />
  </MyContainer>

MyContainer containing B

  <MyContainer>
        <MyObject nameB="foo" attrFromB="bar" />
  </MyContainer>

And MyContainer containing C

  <MyContainer>
        <MyObject nameC="foo" attrFromC="bar" />
  </MyContainer>

So you can already see problems in the horizon…

Here is the mapping file that I would write :

  <?xml version="1.0"?>
     <xml-bindings 
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="com.test.example"
        version="2.1">  

        <java-type name="A" xml-accessor-type="NONE">
           <xml-root-element name="MyObject" />
           <java-attributes>
              <xml-element java-attribute="name" xml-path="@nameA" />
           </java-attributes>
        </java-type>  

        <java-type name="B" xml-accessor-type="NONE">
           <xml-root-element name="MyObject" />
           <xml-see-also>
              com.test.example.A
           </xml.see.also>
           <java-attributes>
              <xml-element java-attribute="name" xml-path="@nameB" />
              <xml-element java-attribute="attrFromB" xml-path="@attrFromB" />
           </java-attributes>
        </java-type>

        <java-type name="C" xml-accessor-type="NONE">
           <xml-root-element name="MyObject" />
           <xml-see-also>
              com.test.example.A
           </xml.see.also>
           <java-attributes>
              <xml-element java-attribute="name" xml-path="@nameC" />
              <xml-element java-attribute="attrFromC" xml-path="@attrFromC" />
           </java-attributes>
        </java-type>

        <java-type name="MyContainer" xml-accessor-type="NONE">
           <xml-root-element name="MyContainer" />
           <java-attributes>
              <xml-element java-attribute="myObject" type="com.test.example.A" xml-path="MyObject" />
           </java-attributes>
        </java-type>

     </xml-bindings>

The first problem is that if I bind the classes like that, I get the following exception :

  [Exception [EclipseLink-44] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
  Exception Description: Missing class indicator field from database row [UnmarshalRecord()].

1st question : I understand that this is normal, Jaxb needs some way to determine the type of MyContaioner.myObject attribute. The problem is that I have no access to the incoming XML files, so I cant add xsi:type fields to them. Is there a way to determine a class based on the presence of a specific attribute in it ? regardless of it’s value. If the source xml contains a @attrFromC attribute, I know the object should be of type C. If it contains attrFromB, it’s B.


The second problem is that the “name” attribute doesn’t exist inside B and C, so jaxb ignores em.

  --Ignoring attribute [name] on class [com.test.example.B] as no Property was generated for it.
  --Ignoring attribute [name] on class [com.test.example.C] as no Property was generated for it.

2nd question : The other problem is that I dont know if Jaxb is capable of overriding xml attribute names like it is expected inside the XML file (@nameA, @nameB and nameC all referring to A.name), is there a way to do it ?

Thanks in advance for your time.

  • 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-28T07:22:24+00:00Added an answer on May 28, 2026 at 7:22 am

    Below are the answers to your questions. The answer to question 2, is also an answer to question 1.


    1st question : I understand that this is normal, Jaxb needs some way
    to determine the type of MyContaioner.myObject attribute. The problem
    is that I have no access to the incoming XML files, so I cant add
    xsi:type fields to them. Is there a way to determine a class based on
    the presence of a specific attribute in it ? regardless of it’s value.
    If the source xml contains a @attrFromC attribute, I know the object
    should be of type C. If it contains attrFromB, it’s B.

    You can leverage the ClassExtractor extension in EclipseLink JAXB (MOXy) for this use case:

    MyClassExtractor

    A ClassExtractor is some code that you can implement to help MOXy determine which class it should instanitate. You are passed a Record and you can ask for the presence of the attributes at the current element by XPath to determine which class should be instantiated.

    package com.test.example;
    
    import org.eclipse.persistence.descriptors.ClassExtractor;
    import org.eclipse.persistence.sessions.*;
    
    public class MyClassExtractor extends ClassExtractor{
    
        @Override
        public Class<?> extractClassFromRow(Record record, Session session) {
            if(null != record.get("@attrFromB")) {
                return B.class;
            } else if(null != record.get("@attrFromC")) {
                return C.class;
            } else {
                return A.class;
            }
        }
    
    }
    

    Metadata (oxm.xml)

    You can configure the ClassExtractor using the @XmlClassExtractor annotation. You can also do this via the external metadata file. I have adapted the one included in your question to include this:

    <?xml version="1.0"?>
    <xml-bindings 
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="com.test.example"
        version="2.3">
        <java-types>
            <java-type name="A" xml-accessor-type="NONE">
               <xml-class-extractor class="com.test.example.MyClassExtractor"/>
               <xml-root-element name="MyObject" />
               <java-attributes>
                  <xml-attribute java-attribute="name" name="nameA" />
               </java-attributes>
            </java-type>  
            <java-type name="B" xml-accessor-type="NONE">
               <xml-root-element name="MyObject" />
               <java-attributes>
                  <xml-attribute java-attribute="name" name="nameB" />
                  <xml-attribute java-attribute="attrFromB"/>
               </java-attributes>
            </java-type>
            <java-type name="C" xml-accessor-type="NONE">
               <xml-root-element name="MyObject" />
               <java-attributes>
                  <xml-attribute java-attribute="name" name="nameC" />
                  <xml-attribute java-attribute="attrFromC"/>
               </java-attributes>
            </java-type>
            <java-type name="MyContainerClass" xml-accessor-type="NONE">
               <xml-root-element name="MyContainer" />
               <java-attributes>
                  <xml-element java-attribute="myObject" name="MyObject" />
               </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    Demo

    The following demo code unmarshals each of the XML documents from your question, and outputs the type being held by the myObject property:

    package com.test.example;
    
    import java.io.StringReader;
    import java.util.*;
    import javax.xml.bind.*;
    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>();
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "com/test/example/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {MyContainerClass.class}, properties);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
    
            StringReader aXml = new StringReader("<MyContainer><MyObject nameA='foo'/></MyContainer>");
            MyContainerClass myContainerA = (MyContainerClass) unmarshaller.unmarshal(aXml);
            System.out.println(myContainerA.getMyObject().getClass());
    
            StringReader bXml = new StringReader("<MyContainer><MyObject nameB='foo' attrFromB='bar'/></MyContainer>");
            MyContainerClass myContainerB = (MyContainerClass) unmarshaller.unmarshal(bXml);
            System.out.println(myContainerB.getMyObject().getClass());
    
            StringReader cXml = new StringReader("<MyContainer><MyObject nameC='foo' attrFromC='bar'/></MyContainer>");
            MyContainerClass myContainerC = (MyContainerClass) unmarshaller.unmarshal(cXml);
            System.out.println(myContainerC.getMyObject().getClass());
        }
    
    }
    

    Output

    [EL Warning]: 2012-01-20 10:36:41.828--Ignoring attribute [name] on class [com.test.example.B] as no Property was generated for it.
    [EL Warning]: 2012-01-20 10:36:41.828--Ignoring attribute [name] on class [com.test.example.C] as no Property was generated for it.
    class com.test.example.A
    class com.test.example.B
    class com.test.example.C
    

    2nd question : The other problem is that I dont know if Jaxb is
    capable of overriding xml attribute names like it is expected inside
    the XML file (@nameA, @nameB and nameC all referring to A.name), is
    there a way to do it ?

    You can leverage an XmlAdapter for this question. This approach can also be used to answer your first question:

    AAdapter

    package com.test.example;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class AAdapter extends XmlAdapter<AAdapter.AdaptedA, A> {
    
        @Override
        public AdaptedA marshal(A a) throws Exception {
            if(null == a) {
                return null;
            }
            AdaptedA adaptedA = new AdaptedA();
            if(a instanceof C) {
                C c = (C) a;
                adaptedA.nameC = c.getName();
                adaptedA.attrFromC = c.getAttrFromC();
            } else if(a instanceof B) {
                B b = (B) a;
                adaptedA.nameB = b.getName();
                adaptedA.attrFromB = b.getAttrFromB();
            } else if(a instanceof A) {
                adaptedA.nameA = a.getName();
            }
            return adaptedA;
        }
    
        @Override
        public A unmarshal(AdaptedA adaptedA) throws Exception {
            if(null == adaptedA) {
                return null;
            }
            if(null != adaptedA.attrFromC) {
                C c = new C();
                c.setName(adaptedA.nameC);
                c.setAttrFromC(adaptedA.attrFromC);
                return c;
            } else if(null != adaptedA.attrFromB) {
                B b = new B();
                b.setName(adaptedA.nameB);
                b.setAttrFromB(adaptedA.attrFromB);
                return b;
            } 
            A a = new A();
            a.setName(adaptedA.nameA);
            return a;
        }
    
        public static class AdaptedA {
            @XmlAttribute public String nameA;
            @XmlAttribute public String nameB;
            @XmlAttribute public String nameC;
            @XmlAttribute public String attrFromB;
            @XmlAttribute public String attrFromC;
        }
    
    }
    

    Metadata (oxm-2.xml)

    <?xml version="1.0"?>
    <xml-bindings 
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="com.test.example"
        version="2.3">
        <java-types>
            <java-type name="MyContainerClass" xml-accessor-type="NONE">
               <xml-root-element name="MyContainer" />
               <java-attributes>
                  <xml-element java-attribute="myObject" name="MyObject">
                    <xml-java-type-adapter value="com.test.example.AAdapter"/>
                  </xml-element>
               </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    Demo2

    package com.test.example;
    
    import java.io.StringReader;
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    public class Demo2 {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "com/test/example/oxm-2.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {MyContainerClass.class}, properties);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            StringReader aXml = new StringReader("<MyContainer><MyObject nameA='foo'/></MyContainer>");
            MyContainerClass myContainerA = (MyContainerClass) unmarshaller.unmarshal(aXml);
            System.out.println(myContainerA.getMyObject().getClass());
            marshaller.marshal(myContainerA, System.out);
    
            StringReader bXml = new StringReader("<MyContainer><MyObject nameB='foo' attrFromB='bar'/></MyContainer>");
            MyContainerClass myContainerB = (MyContainerClass) unmarshaller.unmarshal(bXml);
            System.out.println(myContainerB.getMyObject().getClass());
            marshaller.marshal(myContainerB, System.out);
    
            StringReader cXml = new StringReader("<MyContainer><MyObject nameC='foo' attrFromC='bar'/></MyContainer>");
            MyContainerClass myContainerC = (MyContainerClass) unmarshaller.unmarshal(cXml);
            System.out.println(myContainerC.getMyObject().getClass());
            marshaller.marshal(myContainerC, System.out);
        }
    
    }
    

    Output

    class com.test.example.A
    <?xml version="1.0" encoding="UTF-8"?>
    <MyContainer>
       <MyObject nameA="foo"/>
    </MyContainer>
    class com.test.example.B
    <?xml version="1.0" encoding="UTF-8"?>
    <MyContainer>
       <MyObject nameB="foo" attrFromB="bar"/>
    </MyContainer>
    class com.test.example.C
    <?xml version="1.0" encoding="UTF-8"?>
    <MyContainer>
       <MyObject nameC="foo" attrFromC="bar"/>
    </MyContainer>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.