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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:25:42+00:00 2026-06-05T14:25:42+00:00

How might I marshall an object hierarchy such that, instead of having the component

  • 0

How might I marshall an object hierarchy such that, instead of having the component objects becoming nested XML elements, their properties become direct children of the root element with their names prefixed by their type.

For example, given:

                                                    (A)
public class Customer {

    protected String firstName;
    protected String lastName;
    protected Address address;
}

public class Address {

    protected String street;
    protected String city;
}

Using the usual JAXB annotations would result in

                                                    (B)
<customer>
    <firstName>Juan</firstName>
    <lastName>dela Cruz</lastName>
    <address>
        <street>123 Rizal Avenue</street>
        <city>Manila</city>
    </address>
</customer>

But, instead, I need to marshall the same as

                                                    (C)
<customer>
    <firstName>Juan</firstName>
    <lastName>dela Cruz</lastName>
    <address_street>123 Rizal Avenue</address_street>
    <address_city>Manila</address_city>
</customer>

It would be great if there were some JAXB incantation to solve my needs since I’m already using JAXB for most of the things around this problem. In fact, these present some constraints to my specific situation:

  1. The Java classes in (A) are generated by JAXB from an existing schema that corresponds to the XML structure in (B). I would prefer not to maintain modified versions of the generated classes.
  2. I do not own or maintain the said schema. The actual schema is quite large and is often subject to minor modifications. It would be tedious to come up with and maintain an equivalent schema. Also, to keep up with schema modifications, I rely on the automated class generation by JAXB.
  3. If it makes things easier, nesting is only up to one level deep. In the example, Address would not contain any other complex type.

I’m looking at the @XmlPath annotation from MOXy, but I can’t figure out how to get the node names prefixed as in (C).

I dream of a solution where some xjc customizations providing the right JAXB annotations get me going, but that’s looking unlikely from my searches so far. Any non-JAXB solution would suffice.

  • 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-05T14:25:45+00:00Added an answer on June 5, 2026 at 2:25 pm

    I solved this using an XStream Converter. It checks for the @XmlType annotation to determine if a JAXB bean is being converted. All other types go through the default converters.

    Though a JAXB-centric solution would have been nice, XStream provided a compellingly straightforward solution.

    public class FlatXmlConverter implements Converter {
    
        private static final Logger log =
                LoggerFactory.getLogger(NvpConverter.class);
    
        @Override
        public void marshal(Object source, HierarchicalStreamWriter writer,
                MarshallingContext context) {
            Class<? extends Object> sourceClass = source.getClass();
            String prefix = (String) context.get("prefix");
            for (Field field : sourceClass.getDeclaredFields()) {
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                String name = field.getName();
                Class<?> type = field.getType();
    
                try {
                    Object value = field.get(source);
                    if (value != null) {
                        if (type.isAnnotationPresent(XmlType.class)) {
                            context.put("prefix", name);
                            context.convertAnother(value);
                            context.put("prefix", null);
                        } else {
                            String nodeName;
                            if (prefix == null) {
                                nodeName = name;
                            } else {
                                nodeName = prefix + "_" + name;
                            }
    
                            writer.startNode(nodeName);
                            context.convertAnother(value);
                            writer.endNode();
                        }
                    }
                } catch (IllegalArgumentException ex) {
                    log.error("IllegalArgumentException", ex);
                } catch (IllegalAccessException ex) {
                    log.error("IllegalAccessException", ex);
                }
            }
        }
    
        @Override
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        @Override
        @SuppressWarnings({"rawtypes", "unchecked"})
        public boolean canConvert(Class type) {
            log.debug("canConvert({})", type);
            return type.isAnnotationPresent(XmlType.class);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Might be a bit of noob question but it's something that's been getting me
It might look like a stupid question, but I recently realized that .NET implementation
Might be a simple question, but I can't find an answer that fits this
I have built a component library that includes an executive class which does work
Just as an example, lets say I have a customerX.xml file that I've unmarshalled
Might seem like a stupid question, but I'm having an issue creating an array
This might have been dealt with, so I apologize in advance for that. Anyway,
Might be my question sounds more naive. But I wanted to know if it
Might be my question is abstract or out of context, but i am asking
might be a stupid question but I seem to be confused. Im pretty new

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.