2013-03-13 98 views
1

我將如何映射這個映射嵌套的XML元素,以單一的Java對象

<urn:envelope xmlns:urn="urn:com.twinstrata.webservice:2.1"> 
<urn:encoded> 
    <urn:response> 
     <urn:license> 
      <urn:licenseTag>WHATEVER934</urn:licenseTag> 
      <urn:accountNumber>2016763117</urn:accountNumber> 
      <urn:licenseType>TRIAL</urn:licenseType> 
      <urn:licenseClass>Credentialed</urn:licenseClass> 
      <urn:volumeAllowed>Unlimited</urn:volumeAllowed> 
      <urn:volumeProvisioned>0</urn:volumeProvisioned> 
      <urn:snapshotLimit>Unlimited</urn:snapshotLimit> 
      <urn:snapshotLimitPerVolume>10</urn:snapshotLimitPerVolume> 
      <urn:status>Active</urn:status> 
      <urn:usedSpace>0</urn:usedSpace> 
      <urn:expirtationDate>2013-03-27 14:48:47.0</urn:expirtationDate> 
      <urn:storageLimit>Unlimited</urn:storageLimit> 
     </urn:license> 
    </urn:response> 
</urn:encoded> 
<urn:signature>Hl8rk2aTEsOkkq5e383LH0BqdFfmVcKIg9FuFEnnrlFk9fwYVEQwkrm/7MPM2Zmli2Um00L2Ab25tZg2w8pEzXyDsd+vwCAH0ypQwhIVPayDjgYKlYXbnkqG5S+7qiVbqD2qZDektuPoEWvaSdxO3ZgUibT+nnrO0kl6E7i4lB0= 
</urn:signature> 

這個

package com.folio3.bean; 

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

    @XmlRootElement(name = "envelope" , namespace = "urn:com.twinstrata.webservice:2.1") 
    public class ResponseXML { 


private String userName; 
private String license; 
private String signature; 
private String licenseTag; 
private String accountNumber; 
private String licenseType; 
private String licenseClass; 
private String volumeAllowed; 
private String volumeProvisioned; 
private String publicKey; 

@XmlElement(name = "userName" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getUserName() { 
    return userName; 
} 
public void setUserName(String userName) { 
    this.userName = userName; 
} 

@XmlElement(name = "license" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getLicense() { 
    return license; 
} 
public void setLicense(String license) { 
    this.license = license; 
} 

@XmlElement(name = "signature" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getSignature() { 
    return signature; 
} 
public void setSignature(String signature) { 
    this.signature = signature; 
} 


@XmlElement(name = "licenseTag" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getLicenseTag() { 
    return licenseTag; 
} 
public void setLicenseTag(String licenseTag) { 
    this.licenseTag = licenseTag; 
} 

@XmlElement(name = "accountNumber" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getAccountNumber() { 
    return accountNumber; 
} 
public void setAccountNumber(String accountNumber) { 
    this.accountNumber = accountNumber; 
} 

@XmlElement(name = "licenseType" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getLicenseType() { 
    return licenseType; 
} 
public void setLicenseType(String licenseType) { 
    this.licenseType = licenseType; 
} 

@XmlElement(name = "licenseClass" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getLicenseClass() { 
    return licenseClass; 
} 
public void setLicenseClass(String licenseClass) { 
    this.licenseClass = licenseClass; 
} 

@XmlElement(name = "volumeAllowed" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getVolumeAllowed() { 
    return volumeAllowed; 
} 
public void setVolumeAllowed(String volumeAllowed) { 
    this.volumeAllowed = volumeAllowed; 
} 

@XmlElement(name = "volumeProvisioned" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getVolumeProvisioned() { 
    return volumeProvisioned; 
} 
public void setVolumeProvisioned(String volumeProvisioned) { 
    this.volumeProvisioned = volumeProvisioned; 
} 

@XmlElement(name = "publicKey" , namespace = "urn:com.twinstrata.webservice:2.1") 
public String getPublicKey() { 
    return publicKey; 
} 
public void setPublicKey(String publicKey) { 
    this.publicKey = publicKey; 
} 


@Override 
public String toString() { 
    StringBuilder builder = new StringBuilder(); 
    builder.append("ResponseXML [userName="); 
    builder.append(userName); 
    builder.append(", license="); 
    builder.append(license); 
    builder.append(", signature="); 
    builder.append(signature); 
    builder.append(", licenseTag="); 
    builder.append(licenseTag); 
    builder.append(", accountNumber="); 
    builder.append(accountNumber); 
    builder.append(", licenseType="); 
    builder.append(licenseType); 
    builder.append(", licenseClass="); 
    builder.append(licenseClass); 
    builder.append(", volumeAllowed="); 
    builder.append(volumeAllowed); 
    builder.append(", volumeProvisioned="); 
    builder.append(volumeProvisioned); 
    builder.append(", publicKey="); 
    builder.append(publicKey); 
    builder.append("]"); 
    return builder.toString(); 
} 

}

目前,它映射XML的只有一個屬性,那就是「簽名」。 爲了簡單起見,我不想製作其他類並在其中嵌套對象。我只想解析單個Java類中的嵌套xml標籤。 我該怎麼做?

+2

更改您的xml架構。 – 2013-03-13 16:32:55

回答

-1

我通過創建嵌套類和使用適當的JAXB註釋使它工作。

+0

如果你更具體地瞭解你的解決方案,這將是很好的,它可以幫助其他人......乾杯 – ishanbakshi 2015-07-09 05:16:36

1

備註:我是EclipseLink JAXB (MOXy)的領導者,也是JAXB (JSR-222)專家組的成員。

responseXML的

你可以使用莫西的@XmlPath擴展映射你的使用情況(見:http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html)。以下是您的用例的部分映射。

package forum15391077; 

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

@XmlRootElement(name = "envelope") 
@XmlType(propOrder={"licenseTag", "accountNumber", "licenseType", "licenseClass", "volumeAllowed", "volumeProvisioned", "signature", "license", "publicKey", "userName"}) 
@XmlAccessorType(XmlAccessType.FIELD) 
public class ResponseXML { 

    private String userName; 
    private String license; 
    private String signature; 

    @XmlPath("urn:encoded/urn:response/urn:license/urn:licenseTag/text()") 
    private String licenseTag; 

    @XmlPath("urn:encoded/urn:response/urn:license/urn:accountNumber/text()") 
    private String accountNumber; 

    @XmlPath("urn:encoded/urn:response/urn:license/urn:licenseType/text()") 
    private String licenseType; 

    @XmlPath("urn:encoded/urn:response/urn:license/urn:licenseClass/text()") 
    private String licenseClass; 

    @XmlPath("urn:encoded/urn:response/urn:license/urn:volumeAllowed/text()") 
    private String volumeAllowed; 

    @XmlPath("urn:encoded/urn:response/urn:license/urn:volumeProvisioned/text()") 
    private String volumeProvisioned; 

    private String publicKey; 

} 

包信息

我們將使用軟件包級別@XmlSchema批註指定命名空間資格(見:http://blog.bdoughan.com/2010/08/jaxb-namespaces.html)。我們還將使用它來定義我們在@XmlPath註釋中使用的前綴urn

@XmlSchema(
    namespace="urn:com.twinstrata.webservice:2.1", 
    elementFormDefault=XmlNsForm.QUALIFIED, 
    xmlns={ 
     @XmlNs(namespaceURI = "urn:com.twinstrata.webservice:2.1", prefix = "urn") 
    } 
) 
package forum15391077; 

import javax.xml.bind.annotation.*; 

jaxb.properties

要指定莫西爲您的JAXB提供者,你需要包括在同一個包中的以下項域模型稱爲jaxb.properties文件(參見:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

由於MOXy是標準的JAXB實現,因此使用標準的JAXB運行時API。

package forum15391077; 

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(ResponseXML.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum15391077/input.xml"); 
     ResponseXML response = (ResponseXML) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(response, System.out); 
    } 

} 

的input.xml /輸出

下面是基於我映射你的使用情況的部分示例XML文檔。

<?xml version="1.0" encoding="UTF-8"?> 
<urn:envelope xmlns:urn="urn:com.twinstrata.webservice:2.1"> 
    <urn:encoded> 
     <urn:response> 
     <urn:license> 
      <urn:licenseTag>WHATEVER934</urn:licenseTag> 
      <urn:accountNumber>2016763117</urn:accountNumber> 
      <urn:licenseType>TRIAL</urn:licenseType> 
      <urn:licenseClass>Credentialed</urn:licenseClass> 
      <urn:volumeAllowed>Unlimited</urn:volumeAllowed> 
      <urn:volumeProvisioned>0</urn:volumeProvisioned> 
     </urn:license> 
     </urn:response> 
    </urn:encoded> 
    <urn:signature>Hl8rk2aTEsOkkq5e383LH0BqdFfmVcKIg9FuFEnnrlFk9fwYVEQwkrm/7MPM2Zmli2Um00L2Ab25tZg2w8pEzXyDsd+vwCAH0ypQwhIVPayDjgYKlYXbnkqG5S+7qiVbqD2qZDektuPoEWvaSdxO3ZgUibT+nnrO0kl6E7i4lB0= 
    </urn:signature> 
</urn:envelope> 
+0

你能幫我弄清楚應該在pom.xml中寫入哪些Maven依賴關係來導入 - org.eclipse.persistence.oxm.annotations.XmlPath; – ishanbakshi 2015-07-09 05:12:59