JAVA / JAXB:使用xml或类成员中的属性的Marshal / UnMarshal

时间:2014-10-17 07:08:44

标签: jaxb

我的XML如下

<request type="1">
    <request-header/>
    <request-details>
    <!--  Some more tags -->
    </request-details>
</request>

为了映射这个XML,我有如下类结构:

public class Request1
{
    private RequestDetail_1;
    //other members
}

public class Request2
{
    private RequestDetail_2;
    //other members
} 

public class RequestDetail_1
{
    //members
}

public class RequestDetail_2
{
    //Members
}

我想要做的是......如果属性类型是1,那么我需要创建Request_1类型的对象,如果type为2,那么对象类型将是Request_2,依此类推。

我已通过this链接以供参考,但仍无法找到实现此目的的方法。我想使用纯JAXB而不是MOXY或任何其他这样的框架...... :(。

部分代码:

@XmlJavaTypeAdapter(RequestAdaptor.class)
@XmlRootElement(name="request")
public class AuthRequest extends Request
{
    private AuthRequestDetails requestDetails;

    public RequestDetails getRequestDetails() 
    {
        return requestDetails;
    }

    @Override
    public void setRequestDetails(RequestDetails requestDetails) 
    {
        this.requestDetails = (AuthRequestDetails)requestDetails;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
public class AuthRequestDetails extends RequestDetails
{
    @XmlElement(name="user-name")
    private String userName;
    @XmlElement(name="password")
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlJavaTypeAdapter(RequestAdaptor.class)
public abstract class Request 
{
    @XmlAttribute
    protected String type;

    @XmlElement(name="request-header")
    protected RequestHeader requestHeader;

    public RequestHeader getRequestHeader() 
    {
        return requestHeader;
    }

    public void setRequestHeader(RequestHeader requestHeader) 
    {
        this.requestHeader = requestHeader;
    }

    public String getType() 
    {
        return type;
    }

    public void setType(String type) 
    {
        this.type = type;
    }

    public abstract void setRequestDetails(RequestDetails requestDetails);

    public abstract RequestDetails getRequestDetails();


}

public class RequestAdaptor extends XmlAdapter<RequestDTO, Request>
{
    @Override
    public RequestDTO marshal(Request v) throws Exception 
    {
        System.out.println("marshal");
        RequestDTO lRequestDTO= new RequestDTO();
        lRequestDTO.setRequestHeader(v.getRequestHeader());
        lRequestDTO.setType(v.getType());
        if(v.getType().equals("5"))
        {
            AuthRequest lRequest = (AuthRequest)v;

        }
        else
        {
            PingRequest lRequest = (PingRequest)v;
        }
        return lRequestDTO;
    }

    @Override
    public Request unmarshal(RequestDTO v) throws Exception 
    {
        System.out.println("unmarshal");
        if(v.getType().equals("5"))
        {
            AuthRequest lRequest = new AuthRequest();
            lRequest.setRequestHeader(v.getRequestHeader());
            lRequest.setType(v.getType());
            return lRequest;
        }
        else
        {
            PingRequest lRequest = new PingRequest();
            lRequest.setRequestHeader(v.getRequestHeader());
            lRequest.setType(v.getType());
            return lRequest;
        }
    }

}
@XmlAccessorType(XmlAccessType.FIELD)
public class RequestDTO 
{
    @XmlAttribute
    protected String type;

    @XmlElement(name="request-header")
    private RequestHeader requestHeader;

    @XmlElement(name="request-details")
    private RequestDetails requestDetails;

    public RequestHeader getRequestHeader() 
    {
        return requestHeader;
    }

    public void setRequestHeader(RequestHeader requestHeader) 
    {
        this.requestHeader = requestHeader;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public RequestDetails getRequestDetails() {
        return requestDetails;
    }

    public void setRequestDetails(RequestDetails requestDetails) {
        this.requestDetails = requestDetails;
    }

}

@XmlAccessorType(XmlAccessType.FIELD)
public class RequestHeader 
{
    @XmlElement(name="name")
    String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

}

首先是:适配器的Marshal和Unmarshal没有被调用。我被困在这一点上。

1 个答案:

答案 0 :(得分:0)

您可以使用StAX XmlStreamReader来解析XML。然后将其推进到根元素。当它在根元素事件处检查type属性的值时。使用此值可确定应将Class传递给unmarshal方法,该方法需要ClassXmlStreamReader才能获得您要查找的结果。