Parsing XML to Java Object - Parsing Attributes and Values together

356 Views Asked by At

I have an XML which is something similar to below,

<STANDARD id="1234"> <TOPIC id="111" state_num="ABC111">Reading <TOPIC topicId="222" parent_id="111">Key Ideas and Details</TOPIC> <TOPIC topicId="333" parent_id="111">Key values</TOPIC> </TOPIC> </STANDARD>

Java Object Similar to below,

@XStreamAlias("STANDARD")
public class STANDARD {
    @XStreamAsAttribute
    protected String id;

    @XStreamImplicit
    protected List<MSS.STANDARD.TOPIC> topic;

    @XStreamAlias("TOPIC")
    @XStreamConverter(value=ToAttributedValueConverter.class, strings={"value"})
    public static class TOPIC {

        @XStreamAsAttribute
        protected String topicId;

        @XStreamAsAttribute
        protected String parent_id;

        @XStreamAsAttribute
        protected String value;

        @XStreamImplicit
        protected List<STANDARD.TOPIC> topic;
    }
}

Am facing issues in the following,

  1. The attribute which has an "_" key, ie parent_id is not being parsed.

  2. The values for TOPIC is parsed if I use the XStreamConverter annotation, but the sub TOPIC elements which are present inside TOPIC is not being parsed.

  3. If i remove the XStreamConverter annotation am able to get the Sub Topics parsed by adding Implicit Annotation. But I would like to parse the values as well as the sub elements at the same time.

1

There are 1 best solutions below

0
On

Solution for 1. :

@XStreamAsAttribute
@XStreamAlias("parent_id")
protected String parentId;