1

我有一个带有根元素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>
4

1 回答 1

0

您可以使用带Class参数的解组方法之一来告诉 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();
于 2013-02-20T22:09:24.223 回答