[xstream-user] Abstract class and field prefix problem

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[xstream-user] Abstract class and field prefix problem

Phil Hayes

Hi All,

I am having a problem reading an XML file into a class that extends an abstract class
I am trying to add a prefix to the XML element names to match the class properties, for which I use a FieldPrefixStrippingMapper, code is attached.
I find that I can only read the data into the POJO quite OK.
But when I try to extend a class, I can not seem to populate any base class properties unless I specify the XML element name exactly as the base class property name.
What do I need to do to allow the prefix to be added to the base class property name before the code is unmarshalled ?
Thank-you very much for your time and assistance and a great product.


=== this is the code to read the seralized file =====

XStream xs = new XStream(new Sun14ReflectionProvider()) {
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new FieldPrefixStrippingMapper(next);


cmmsCheckConfig cc = (cmmsCheckConfig)xs.fromXML(new java.io.FileReader("alerting_config.xml"));

========= here is the the code for the FieldPrefixStrippingMapper class =================

package au.com.cmms.Util;

import com.thoughtworks.xstream.alias.ClassMapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;

     * A sample mapper strips the underscore prefix of fieldnames in the XML
    public class FieldPrefixStrippingMapper extends MapperWrapper {
        public FieldPrefixStrippingMapper(ClassMapper wrapped) {

        public String serializedMember(Class type, String memberName) {
            if (memberName.startsWith("its")) {
                // _blah -> blah
                memberName = memberName.substring(3); // chop off leading char (the underscore)
            return super.serializedMember(type, memberName);

        public String realMember(Class type, String serialized) {
            String fieldName = super.realMember(type, serialized);
            // Not very efficient or elegant, but enough to get the point across.
            // Luckily the CachingMapper will ensure this is only ever called once per field per class.
            try {
                type.getDeclaredField("its" + fieldName);
                return "its" + fieldName;
            } catch (NoSuchFieldException e) {
                return fieldName;

This e-mail is confidential.  If you are not the intended recipient, any use, disclosure or copying of this document is unauthorised and prohibited.  If you have received this document in error, please delete the email and notify me by return email or by phoning the NEMMCO Helpdesk on 1300 300 295.