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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:29:52+00:00 2026-05-15T15:29:52+00:00

Is it possible to use JAXB to unmarshall xml to a specific Java class

  • 0

Is it possible to use JAXB to unmarshall xml to a specific Java class based on an attribute of the xml?

<shapes>
  <shape type="square" points="4" square-specific-attribute="foo" />
  <shape type="triangle" points="3" triangle-specific-attribute="bar" />
</shapes>

I would like to have a List of Shape objects containing a triangle and a square, each with their own shape-specific attribute. IE:

abstract class Shape {
    int points;
    //...etc
}

class Square extends Shape {
    String square-specific-attribute;
    //...etc
}

class Triangle extends Shape {
    String triangle-specific-attribute;
    //...etc
}

I’m currently just putting all attributes in one big “Shape” class and it’s less than ideal.

I could get this to work if the shapes were properly named xml elements, but unfortunately I don’t have control of the xml I’m retrieving.

Thanks!

  • 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-15T15:29:52+00:00Added an answer on May 15, 2026 at 3:29 pm

    JAXB is a spec, specific implementations will provide extension points to do things such as this. If you are using EclipseLink JAXB (MOXy) you could modify the Shape class as follows:

    import javax.xml.bind.annotation.XmlAttribute;
    import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
    
    @XmlCustomizer(ShapeCustomizer.class)
    public abstract class Shape {
    
        int points;
    
        @XmlAttribute
        public int getPoints() {
            return points;
        }
    
        public void setPoints(int points) {
            this.points = points;
        }
    
    }
    

    Then using the MOXy @XMLCustomizer you could access the InheritancePolicy and change the class indicator field from “@xsi:type” to just “type”:

    import org.eclipse.persistence.config.DescriptorCustomizer;
    import org.eclipse.persistence.descriptors.ClassDescriptor;
    
    public class ShapeCustomizer implements DescriptorCustomizer {
    
        @Override
        public void customize(ClassDescriptor descriptor) throws Exception {
            descriptor.getInheritancePolicy().setClassIndicatorFieldName("@type");
        }
    }
    

    You will need to ensure that you have a jaxb.properties file in with you model classes (Shape, Square, etc) with the following entry specifying the EclipseLink MOXy JAXB implementation:

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

    Below is the rest of the model classes:

    Shapes

    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Shapes {
    
        private List<Shape> shape = new ArrayList<Shape>();;
    
        public List<Shape> getShape() {
            return shape;
        }
    
        public void setShape(List<Shape> shape) {
            this.shape = shape;
        }
    
    }
    

    Square

    import javax.xml.bind.annotation.XmlAttribute;
    
    public class Square extends Shape {
        private String squareSpecificAttribute;
    
        @XmlAttribute(name="square-specific-attribute")
        public String getSquareSpecificAttribute() {
            return squareSpecificAttribute;
        }
    
        public void setSquareSpecificAttribute(String s) {
            this.squareSpecificAttribute = s;
        }
    
    }
    

    Triangle

    import javax.xml.bind.annotation.XmlAttribute;
    
    public class Triangle extends Shape {
        private String triangleSpecificAttribute;
    
        @XmlAttribute(name="triangle-specific-attribute")
        public String getTriangleSpecificAttribute() {
            return triangleSpecificAttribute;
        }
    
        public void setTriangleSpecificAttribute(String t) {
            this.triangleSpecificAttribute = t;
        }
    
    }
    

    Below is a demo program to check that everything works:

    import java.io.StringReader;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jaxbContext = JAXBContext.newInstance(Shapes.class, Triangle.class, Square.class);
    
            StringReader xml = new StringReader("<shapes><shape square-specific-attribute='square stuff' type='square'><points>4</points></shape><shape triangle-specific-attribute='triangle stuff' type='triangle'><points>3</points></shape></shapes>");
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Shapes root = (Shapes) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    }
    

    I hope this helps.

    For more information on EclipseLink MOXy see:

    • http://www.eclipse.org/eclipselink/moxy.php

    EDIT

    In EclipseLink 2.2 we’re making this easier to configure, check out the following article for more information:

    • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-moxy-extension.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Oh, I got it. The Resource was an anonymous class,… May 16, 2026 at 2:07 pm
  • Editorial Team
    Editorial Team added an answer Vbscript might look similar to VB.NET but there's a difference.… May 16, 2026 at 2:07 pm
  • Editorial Team
    Editorial Team added an answer I can think of two options: You can use NSMutableArray's… May 16, 2026 at 2:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following java class with the JAXB @XMLRootElement annotation @XmlRootElement(name=ClientData) public class
I'm trying to use a solution for serializing exceptions using jaxb. ( http://forums.java.net/jive/thread.jspa?messageID=256122 )
Is it possible to use Jackson as the serializer/marshaller for JSON data instead of
Is it possible to create a web service operation using primitive or basic Java
Possible Duplicate: Use of var keyword in C# Which declaration should be accepted ?
Is it possible to use style for .net winform control ?
Is it possible to use CriteriaQuery with JPA 1.0. I guess JPA 2.0 not
Is it possible to use windbg commands like !locks and .loadby sos mscorwks from
Is it possible to use PHP alternate syntax (as described in the CodeIgniter user
Is it possible to use an object as a hash key? For example, the

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.