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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:42:48+00:00 2026-05-15T22:42:48+00:00

There’s ugly XML file that has to be unmarshalled: <?xml version=1.0 ?> <configuration> <section

  • 0

There’s ugly XML file that has to be unmarshalled:

<?xml version="1.0" ?>
<configuration>
    <section name="default_options">
        <value name="default_port">8081</value>
        <value name="log_level">WARNING</value>
    </section>
    <section name="custom_options">
        <value name="memory">64M</value>
        <value name="compatibility">yes</value>
    </section>
</configuration>

Resulting Java Objects should be:

public class DefaultOptions {
    private int defaultPort;
    private String logLevel;
    // etc...
}

public class CustomOptions {
    private String memory;
    private String compatibility;
    // etc...
}

This question’s answer is very close but I can’t figure out the final solution.

  • 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-15T22:42:48+00:00Added an answer on May 15, 2026 at 10:42 pm

    How about?

    Introduce a common super class called Options:

    import javax.xml.bind.annotation.XmlAttribute;
    
    public abstract class Options {
    
        private String name;
    
        @XmlAttribute
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    Then on your class with the list of options (Configuration in this example), specify an @XmlJavaTypeAdapter on that property:

    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Configuration {
    
        private List<Options> section = new ArrayList<Options>();
    
        @XmlJavaTypeAdapter(OptionsAdapter.class)
        public List<Options> getSection() {
            return section;
        }
    
        public void setSection(List<Options> section) {
            this.section = section;
        }
    
    }
    

    The XmlAdapter will look something like this:

    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class OptionsAdapter extends XmlAdapter<AdaptedOptions, Options> {
    
        @Override
        public Options unmarshal(AdaptedOptions v) throws Exception {
            if("default_options".equals(v.name)) {
                DefaultOptions options = new DefaultOptions();
                options.setName(v.getName());
                options.setDefaultPort(Integer.valueOf(v.map.get("default_port")));
                options.setLogLevel(v.map.get("log_level"));
                return options;
            } else {
                CustomOptions options = new CustomOptions();
                options.setName(v.getName());
                options.setCompatibility(v.map.get("compatibility"));
                options.setMemory(v.map.get("memory"));
                return options;
            }
        }
    
        @Override
        public AdaptedOptions marshal(Options v) throws Exception {
            AdaptedOptions adaptedOptions = new AdaptedOptions();
            adaptedOptions.setName(v.getName());
            if(DefaultOptions.class == v.getClass()) {
                DefaultOptions options = (DefaultOptions) v;
                adaptedOptions.map.put("default_port", String.valueOf(options.getDefaultPort()));
                adaptedOptions.map.put("log_level", options.getLogLevel());
            } else {
                CustomOptions options = (CustomOptions) v;
                adaptedOptions.map.put("compatibility", options.getCompatibility());
                adaptedOptions.map.put("memory", options.getMemory());
            }
            return adaptedOptions;
        }
    
    }
    

    AdaptedOptions looks like:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlValue;
    
    public class AdaptedOptions extends Options {
    
        @XmlAttribute String name;
        @XmlElement List<Value> value = new ArrayList<Value>();
        Map<String, String> map = new HashMap<String, String>();
    
        public void beforeMarshal(Marshaller marshaller) {
            for(Entry<String, String> entry : map.entrySet()) {
                Value aValue = new Value();
                aValue.name = entry.getKey();
                aValue.value = entry.getValue();
                value.add(aValue);
            }
        }
    
        public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
            for(Value aValue : value) {
                map.put(aValue.name, aValue.value);
            }
        }
    
        private static class Value {
            @XmlAttribute String name;
            @XmlValue String value;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a website called Gild.com that has different coding puzzles/challenges for users to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
There is a table view with three sections. The last section may contain many
I've got a string that has curly quotes in it. I'd like to replace
There is no doubt that MonoTouch is one of the great cross-compiler(s). Similarly, SenchaTouch
There is a text file with 1,000,000 lines of code floating around and Ctrl+F
There is a class that's a fully fledged UIViewController and when that page loads
There is a security concept regarding the calls to the WCF service that I
There are many tutorials that talk about deleting index.php from the url. But I
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.