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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:00:41+00:00 2026-06-17T16:00:41+00:00

I have a configuration file in XML, which looks something like: <configuration> <database> <host></host>

  • 0

I have a configuration file in XML, which looks something like:

<configuration>
     <database>
          <host></host>
          <port></port>
     </database>
     <queue>
           <host></host>
           <port></port>
           <type></type>
      </queue>
</configuration>

I would like to use JAXB/xjc to generate Java classes for this configuration, however I would like to generate these classes and unmarshal these one level into the tree. Rather than receiving a Configuration.java, I would like a Database.java and Queue.java (in order that these can be separately injected into a Guice managed application). I don’t (currently) see any way of doing this, however may be searching for the wrong things.


After some experimentation, I have found a solution to generate these classes and be able to populate and return these based on class:

First, I added a bindings.xjb file which will generate the contained classes (Database and Queue in this example)

<jaxb:bindings
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:globalBindings localScoping="toplevel"/>
</jaxb:bindings>

However, JAXB can’t unmarshall using the Database or Queue class, only the Configuration class (this is where I may have missed something). I can do

JAXBContext context = JAXBContext.newInstance(Configuration.class);
Unmarshaller um = context.createUnmarshaller();
Configuration conf = (Configuration) um.unmarhal(xmlFile);

but not

JAXBContext context = JAXBContext.newInstance(Database.class);
Unmarshaller um = context.createUnmarshaller();
Database db = (Database) um.unmarhal(xmlFile);

However, because I can get the database object by calling getDatabase() on an instance of the Configuration object, this can also be made generic using reflection (making this code cache results in the appropriate places is another exercise):

    T item = null;
    try {
        JAXBContext context = JAXBContext.newInstance(Configuration.class);
        Unmarshaller um = context.createUnmarshaller();
        Configuration conf = (Configuration) um.unmarshal(xmlFile);
        Method[] allMethods = Configuration.class.getDeclaredMethods();
        for (Method method : allMethods)
        {
            if (method.getReturnType().equals(clazz))
            {
                item = (T) method.invoke(conf);
                break;
            }
        }
    } catch (JAXBException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new ConfigException("Failure detected while loading configuration", e);
    }
    return item;

I’m not sure this is the best solution (I only started working with JAXB yesterday), but seems to fulfill my goal.

  • 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-17T16:00:42+00:00Added an answer on June 17, 2026 at 4:00 pm

    I am answering this question with my solution to the problem as discussed in the question. This meets my requirements:


    After some experimentation, I have found a solution to generate these classes and be able to populate and return these based on class:

    First, I added a bindings.xjb file which will generate the contained classes (Database and Queue in this example)

    <jaxb:bindings
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        version="2.1">
        <jaxb:globalBindings localScoping="toplevel"/>
    </jaxb:bindings>
    

    However, JAXB can’t unmarshall using the Database or Queue class, only the Configuration class (this is where I may have missed something). I can do

    JAXBContext context = JAXBContext.newInstance(Configuration.class);
    Unmarshaller um = context.createUnmarshaller();
    Configuration conf = (Configuration) um.unmarhal(xmlFile);
    

    but not

    JAXBContext context = JAXBContext.newInstance(Database.class);
    Unmarshaller um = context.createUnmarshaller();
    Database db = (Database) um.unmarhal(xmlFile);
    

    However, because I can get the database object by calling getDatabase() on an instance of the Configuration object, this can also be made generic using reflection (making this code cache results in the appropriate places is another exercise):

        T item = null;
        try {
            JAXBContext context = JAXBContext.newInstance(Configuration.class);
            Unmarshaller um = context.createUnmarshaller();
            Configuration conf = (Configuration) um.unmarshal(xmlFile);
            Method[] allMethods = Configuration.class.getDeclaredMethods();
            for (Method method : allMethods)
            {
                if (method.getReturnType().equals(clazz))
                {
                    item = (T) method.invoke(conf);
                    break;
                }
            }
        } catch (JAXBException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new ConfigException("Failure detected while loading configuration", e);
        }
        return item;
    

    This allows me to get an unmarshalled Database by passing Database.class without needing to hardcode methods for ever configuration parameter. These can then be injected where needed without needing to inject the entire unmarshalled XML file.

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

Sidebar

Related Questions

I have a config file which lists all the configuration details for Logback.xml like
I have a file named configuration.xml which resides in classes folder of my WEB-INF
I have some values in a configuration file (XML file) with some values like
I have a database configuration file, database.yml, which needs to be in Git because
I have an xml configuration file which works fine in Java, and I'm trying
What I would like to do is create a configuration/properties/xml file in an Eclipse
I have an XML configuration file to which I will occasionally add new elements
I'm using apache commons library and log4j. I have an xml configuration file and
I have the following configuration file for NHibernate : <?xml version=1.0 encoding=utf-8 ?> <hibernate-configuration
I have an ANT configuration file which is becoming complicated, and now I'm stuck

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.