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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:12:19+00:00 2026-06-09T02:12:19+00:00

I am using XStream to convert a Java class which has fields of the

  • 0

I am using XStream to convert a Java class which has fields of the type java.util.Map. I have a converter for java.util.Map which displays the key of the Map as an xml element and the value of the map as the value for the xml element. I have registered the converter using registerConverter method. When I perform the marshalling, I get the following output.

<cart account_id="123" shift_id="456" account_postings_id="641">
  <supervisor_id>555</supervisor_id>
  <payments>
    <map sequence="1">
      <amount>123.45</amount>
      <billing_method>12345</billing_method>
      <form>card</form>
      <delivery_mode>Q</delivery_mode>
    </map>
    <map sequence="2">
      <amount>123.45</amount>
      <person_id>2333</person_id>
      <form>cash</form>
      <delivery_mode>Q</delivery_mode>
     </map>
  </payments>
  <items>
    <map sequence="3">
      <amount>1.00</amount>
      <type>pay_toll</type>
      <toll_id>1234</toll_id>
    </map>
  </items>
</cart>

Instead of the map tags appearing, I would want to use different tags based on the field name in the class. For example, the Payments list will have tag name payment and the Items list will have a tag name item for each Map element.

How do we dynamically set alias based on field in the same class?

-Anand

  • 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-09T02:12:20+00:00Added an answer on June 9, 2026 at 2:12 am

    I used XStream to create atom feed reports. Entries in the contents can be of different object classes and I wanted to use the class name dynamically. Here is my solution. I created a ObjectContentConverter and passed the XStream on, and then use xstream.aliasField() with getClass().getSimpleName().

    private class ObjectContentConverter implements Converter {
        XStream xStream;
    
        private ObjectContentConverter(XStream xStream) {
            this.xStream = xStream;
        }
    
        @Override
        public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
            String className = WordUtils.uncapitalize(source.getClass().getSimpleName());
            xStream.aliasField(className, Content.class, "objectContent");
            xStream.marshal(source, writer);
        }
    
        @Override
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }
    
        @Override
        public boolean canConvert(Class type) {
            return true;  //To change body of implemented methods use File | Settings | File Templates.
        }
    }
    
    xStream.registerLocalConverter(Content.class, "objectContent", new ObjectContentConverter(xStream));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning on using XStream with Java to convert between objects and XML
I'm using XStream to convert java objects to their XML counterparts. One such object
One of my class has to serialize huge numbers of instances and using Xstream,
I have a simple java String object and wanna serialize/deserialize it, using XStream. Serialization
I have a xml file, which is not serialized using XStream. It may be
I am using nice little piece of xstream to perform serialization. I have the
I am trying to flatten the xml output of xstream using a converter/marshaling with
I have a class MyClass . If I serialize it without implementing custom converter
We'd like to generate xml using xstream implemented with java. We'd then like to
I'm using Xstream to serialize a EJB entity class to be transferred by a

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.