使用MOXy解组到不同的对象

时间:2013-02-20 18:53:50

标签: jaxb eclipselink moxy unmarshalling

我有一个带有根元素 SearchResourceResponse 的响应xml。我需要将其解组为另一个自定义(HSIDetails)对象。我开始使用MOXy作为我的JAXB(JSR-222)实现。 我的下面内涵是否正确?这可能是MOXy ??

JAXBContext jc = JAXBContext.newInstance(HSIDetails.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
HSIDetails hsiDetails = (HSIDetails) unmarshaller.unmarshal(new StreamSource(new StringReader(responseXml)));

和我的HSIDetails类

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class HSIDetails implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -4352912510533245455L;

    @XmlPath("SearchResponseDetails/LogicalDevice/LogicalPhysicalResource/PhysicalResource[@*[local-name()='type' and contains(.,'icl:Slot')]]/commonName")
    private String slot;
    @XmlPath("SearchResponseDetails/LogicalDevice/LogicalPhysicalResource/PhysicalResource[@*[local-name()='type' and contains(.,'icl:PhysicalPort')]]/commonName")
    private String port;

    @XmlPath("SearchResponseDetails/SubNetwork/Pipe/commonName")
    private String telephone;

    @XmlPath("//SearchResponseDetails/SubNetwork/Pipe/lrStatus")
    private String lrStatus;

    public String getSlot() {
        return slot;
    }
    public void setSlot(String slot) {
        this.slot = slot;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getLrStatus() {
        return lrStatus;
    }
    public void setLrStatus(String lrStatus) {
        this.lrStatus = lrStatus;
    }
}

我的部分xml是:

<tns:SearchResourceResponse xmlns:tns="http://www.ICLNBI.com/ICLNBI.xsd">
    <SearchResponseDetails>
        <SubNetwork>
            <Pipe xsi:type="icl:Trail" xmlns:icl="http://www.ICLNBI.com/ICLNBI.xsd"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <CommonName>XXXXX</CommonName>
                <objectID>1234567890</objectID>
                <description>512/2</description>
                <SourceSystem>YYYY</SourceSystem>
            </Pipe>
        </SubNetwork>
    </SearchResponseDetails>
</tns:SearchResourceResponse>

1 个答案:

答案 0 :(得分:0)

你可以使用一个带有Class参数的unmarshal方法来告诉MOXy或任何JAXB(JSR-222)实现要解组的类。

 JAXBContext jc = JAXBContext.newInstance(HSIDetails.class);
 Unmarshaller unmarshaller = jc.createUnmarshaller();
 HSIDetails hsiDetails = unmarshaller.unmarshal(
     new StreamSource(new StringReader(responseXml)), 
     HSIDetails.class).getValue();