使用Jersey解析子节点

时间:2010-09-14 11:45:25

标签: java xml jaxb jersey

我们正在使用Jersey连接到第三方。然后我们想要将返回的xml提取到我们的类中。这实际上工作正常,除了xml中的一个节点在子节点中。 这是返回的xml:

<response>  
...  
<langISO>en</langISO>  
<acquirerAmount>1000</acquirerAmount>  
<acquirerCurrency>GBP</acquirerCurrency>
<subXml>  
<authCode>122958</authCode>  
</subXml>  
</response>

请注意,authCode节点位于子节点(称为subXml)。

OurResponse myriadResponse = response.getEntity(OurResponse.class);

这是我们的类,但它没有解析出authCode

package com.xxx;  

import javax.ws.rs.Consumes;  
import javax.ws.rs.Path;  
import javax.xml.bind.annotation.XmlElement;  
import javax.xml.bind.annotation.XmlRootElement;  

@XmlRootElement  
@Consumes("application/xml")  
public class OurResponse {  
    private String authCode;  

    @XmlElement(name = "subXml/authCode")  
    public String getAuthCode() {  
        return authCode;  
    }  

    @XmlElement(name = "subXml/authCode")  
    public void setAuthCode(String authCode) {  
        this.authCode = authCode;  
    }  
}

2 个答案:

答案 0 :(得分:1)

您有两种不同的选择:

选项1 - MOXy JAXB&amp; @XmlPath

您可以使用MOXy JAXB实现和@XmlPath扩展来实现所需的结果:

import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="response")
public class OurResponse {
    private String authCode;

    @XmlPath("subXml/authCode/text()")
    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

有关详细信息,请参阅:

选项2 - 任何JAXB Impl和@XmlJavaTypeAdapter

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="response")
public class OurResponse {
    private String authCode;

    @XmlJavaTypeAdapter(AuthCodeAdapter.class)
    @XmlElement(name="subXml")
    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class AuthCodeAdapter extends XmlAdapter<SubXml, String> {

    @Override
    public String unmarshal(SubXml v) throws Exception {
        return v.getAuthCode();
    }

    @Override
    public SubXml marshal(String v) throws Exception {
        SubXml subXml = new SubXml();
        subXml.setAuthCode(v);
        return subXml;
    }

}

public class SubXml {

    private String authCode;

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

有关详细信息,请参阅:

答案 1 :(得分:0)

我认为你不能像这样用XmlElement注释。您可能需要创建一个单独的SubXml类;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="response")
public class OurResponse
{
    private String lang;
    private String amt;
    private String curr;
    private SubXml subXml;

    public OurResponse()
    {

    }

    //Getters and setters
}

public class SubXml
{
    private String authcode;

    public SubXml()
    {

    }

    public String getAuthcode()
    {
        return authcode;
    }

    public void setAuthcode(String authcode)
    {
        this.authcode = authcode;
    }
}

请注意,您需要的唯一注释是OurResponse类上的@XmlRootElement,您需要设置名称; name="response"