3

I am new to Simple XML, but I have encountered problem with population of unwanted objects in inner elements.

The actual problem is that I want to have this hierarchy:

<Request>
    <SpecificRequest>
         <UniqueID></UniqueID>
             or
         <Password></Password>
    </SpecificRequest>
</Request>

But have this:

<Request>
     <SpecificRequest>
        <mData class="com.example.package.UIDData">
           <UniqueID>6252859A</UniqueID>
        </mData>
     </SpecificRequest >
</Request>

Classes look like this:

Root request object:

@Root(name = "Request")
public class Request {
    @Element(name = "SpecificRequest")
    private SpecificRequest mSpecificRequest;

    public Request(SpecificRequest specificRequest) {
        mSpecificRequest = specificRequest;
    }
}

Specific request object:

public class SpecificRequest {
    private Data mData;

    public SpecificRequest(Data data) {
        mData = data;
    }
}

Generic data class:

public abstract class Data {
    private Type mType = Type.None;

    protected Data(Type type) {
        mType = type;
    }

    // other abstract stuff
}

And specific data class:

public class UIDData extends Data {
    @Element(name = "UniqueID")
    private String mUID;

    public UIDData(String UID) {
        super(Type.UID);
        mUID = UID;
    }
}

P.S. This is not duplication of this or this.

1 Answer 1

1

Add UIDData.class as type in SimpleXml @Element annotation

public class TestClass {

private String getXmlBody(Object o, Matcher matcher) throws Exception {
        Format format = new Format(4, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        Serializer serializer = new Persister(new AnnotationStrategy(), matcher, format);
        Writer writer = new StringWriter();
        serializer.write(o, writer);
        writer.write('\n');
        return writer.toString();
    }    
 public static void main(String[] args) throws Exception {
 Data data = new UIDData("some UUID value ");
 SpecificRequest specificRequest  =  new SpecificRequest(data);
 Request request = new Request(specificRequest);
 System.out.println(getXmlBody(request, new RegistryMatcher()));
        }
    }
    @Root(name = "Request")
    class Request {
        @Element(name = "SpecificRequest")
        private SpecificRequest mSpecificRequest;

        public Request(SpecificRequest specificRequest) {
            mSpecificRequest = specificRequest;
        }
    }
    class SpecificRequest {
     @Element(type = UIDData.class)
        private Data mData;

        public SpecificRequest(Data data) {
            mData = data;
        }
    }

    abstract class Data {
        private String mType = "";

        protected Data(String type) {
            mType = type;
        }

        // other abstract stuff
    }

    class UIDData extends Data {
        @Element(name = "UniqueID")
        private String mUID;

        public UIDData(String UID) {
            super("UID");
            mUID = UID;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.