1
我有一個響應xml和根元素SearchResourceResponse。我需要將它解組爲不同的定製對象(HSIDetails)。我開始使用MOXy作爲我的JAXB(JSR-222)實現。 是我下面的內涵正確嗎?這可能與MOXY?使用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>
謝謝@Blaise Doughan,它確實工作,但值爲空。我將下面的命名空間添加到package-info中,並更新了我的xpaths。 ** package-info ** \t'@ javax.xml.bind.annotation.XmlSchema(namespace =「http://www.ICLNBI.com/ICLNBI.xsd」, \t xmlns = { \t @ javax.xml。 bind.annotation.XmlNs(namespaceURI =「http://www.ICLNBI.com/ICLNBI.xsd」,prefix =「ns2」) \t} \t)'我的xpath看起來像'@XmlPath(「ns2:searchResourceResponse/SearchResponseDetails/SubNetwork/Pipe/CommonName/text()「)\t private String telephone'; – VJay 2013-02-20 23:16:47
我只是無法得到它的上述XML工作,我錯過了什麼?無論我給出哪個xml路徑,都會使用null值。我相信這個問題不在註釋之中..其他東西.. – VJay 2013-02-22 20:50:58
@ user2092307 - 你確定MOXy是作爲JAXB提供者引入的嗎?如果你做'System.out.println(jc.getClass());'你會得到什麼? – 2013-02-22 20:59:52