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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:07:52+00:00 2026-05-31T18:07:52+00:00

I have XML structure. <cars> <car name = BMW engine=2.5/> <car name = Lexus

  • 0

I have XML structure.

<cars>
    <car name = "BMW" engine="2.5"/>
    <car name = "Lexus" engine="4.5"/>
    <car name = "VW" engine="1.4"/>
    <car name = "Honda" engine="2.0"/>
</cars>

I have Java classes for each of car model.

public class BMW extends Car{

    public BMW(){


    }


}

How do I design my main() class to parse this XML and atomatically invoke constructor for required Car. Lets say I get a node <.car name = “BMW” engine=”2.5″/> this means I want to invoke BMW constructor create a BMW object and store everything into List<.Car>.

Thanks for any tips! 🙂

  • 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-31T18:07:54+00:00Added an answer on May 31, 2026 at 6:07 pm

    You could map this use case using any JAXB (JSR-222) by taking advantage of an XmlAdapter:

    CarAdapter

    In your example you are using a custom node as the inheritance indicator. Using the standard JAXB APIs we can use an XmlAdapter to map this use case. An XmlAdapter converts from a domain object to an object that is easier for the JAXB implementation (Metro, MOXy, JaxMe, etc) to map.

    package forum9812778;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class CarAdapter extends XmlAdapter<CarAdapter.AdaptedCar, Car> {
    
        @Override
        public AdaptedCar marshal(Car car) throws Exception {
            AdaptedCar adaptedCar = new AdaptedCar();
            adaptedCar.name = car.getClass().getSimpleName();
            adaptedCar.engine = car.getEngine();
            return adaptedCar;
        }
    
        @Override
        public Car unmarshal(AdaptedCar adaptedCar) throws Exception {
            Car car;
            if("BMW".equals(adaptedCar.name)) {
                car = new BMW();
            } else if("Lexus".equals(adaptedCar.name)) {
                car = new Lexus();
            } else if("VW".equals(adaptedCar.name)) {
                car = new VW();
            } else if("Honda".equals(adaptedCar.name)) {
                car = new Honda();
            } else {
                return null;
            }
            car.setEngine(adaptedCar.engine);
            return car;
        }
    
        public static class AdaptedCar {
            @XmlAttribute
            public String name;
    
            @XmlAttribute
            public String engine;
        }
    
    }
    

    Car

    The @XmlJavaTypeAdapter annotation is used to associate the XmlAdapter with the Car class:

    package forum9812778;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    @XmlJavaTypeAdapter(CarAdapter.class)
    public class Car {
    
        private String engine;
    
        public String getEngine() {
            return engine;
        }
    
        public void setEngine(String engine) {
            this.engine = engine;
        }
    
    }
    

    BMW

    Below is an example of one of the subclasses.

    package forum9812778;
    
    public class BMW extends Car {
    
    }
    

    Cars

    We need an Object to represent the root node in our tree. We will define the Cars class to serve this role:

    package forum9812778;
    
    import java.util.List;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Cars {
    
        private List<Car> car;
    
        public List<Car> getCar() {
            return car;
        }
    
        public void setCar(List<Car> car) {
            this.car = car;
        }
    
    }
    

    Demo

    package forum9812778;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Cars.class);
    
            File xml = new File("src/forum9812778/input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Cars cars = (Cars) unmarshaller.unmarshal(xml);
    
            for(Car car : cars.getCar()) {
                System.out.println(car.getClass());
            }
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(cars, System.out);
        }
    
    }
    

    Output

    class forum9812778.BMW
    class forum9812778.Lexus
    class forum9812778.VW
    class forum9812778.Honda
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <cars>
        <car engine="2.5" name="BMW"/>
        <car engine="4.5" name="Lexus"/>
        <car engine="1.4" name="VW"/>
        <car engine="2.0" name="Honda"/>
    </cars>
    

    For More Information

    • http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bad xml structure file: <cars> <car>Toyota <country>Japan</coutry> .... </car> </cars> How
Is it possible to change given XML structure <Cars> <Car>Honda</Car> <Car>Ferrari</Car> </Cars> with XLST
I have xml structure like this: <Group id=2 name=Third parentid=0 /> <Group id=6 name=Five
So i have this XML structure: <fields> <field name=agent_description label=Agent Description size=area /> <field
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
Back again with another Flex question. I have an XML structure like... <Student> <Name>X</Name>
I have the following XML structure: <node name=A> <node name=B> <node name=C/> <node name=D/>
I have the following XML structure: <row> <field name=Id>1</field> <field name=AreaId>1</field> <field name=Name>ת&quot;א</field> </row>
I have a XML structure like the following: <Root> <Node Name=File System> <Node Name=C:\Windows\System32\drivers\etc\hosts>
I have this XML structure: <numb> <variable> <name>john</name> <age>12</age> </variable> <variable> <name>janet</name> <age>10</age> </variable>

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.