开发者

Mapping mixed content type using XStream

开发者 https://www.devze.com 2023-01-15 09:12 出处:网络
A question on creating java class for following XML node which contains a error code and description on the same element. My question is about how could I map the error message details in the java cla

A question on creating java class for following XML node which contains a error code and description on the same element. My question is about how could I map the error message details in the java class so XStream works for deserializing.

XML:

<response code="failure">
   <![CDATA[error message details...]]>
</response>

Java:

@XStreamAlias("response")
p开发者_运维问答ublic class ErrorResponse {
 @XStreamAlias("code")
    @XStreamAsAttribute
 private String code;  
....
....
}

Thanks.


The following may be what you are looking for:

  • XStream : node with attributes and text node?

However, I'll point out what you are trying to do is much easier with JAXB:

import javax.xml.bind.annotation.*;

@XmlRootElement(name="response")
@XmlAccessorType(XmlAccessType.FIELD)
public class ErrorResponse {

    @XmlAttribute
    private String code;

    @XmlValue
    private String description;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消