2013-03-13 2 views
1

내가 현재이매핑 중첩 된 XML 요소

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(); 
} 

}

<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> 

을지도 할 방법은 XML의 한 속성을 매핑 , 그것은 "서명"입니다. 편의상, 다른 클래스를 만들고 그 안에 객체를 중첩하지 않으려합니다. 난 그냥 단일 Java 클래스에서 중첩 된 XML 태그를 구문 분석 싶어요. 어떻게하면됩니까?

+2

xml 스키마를 변경하십시오. –

답변

-1

중첩 된 클래스를 만들고 올바른 JAXB 주석을 사용하여 작동하도록 만들었습니다.

+0

당신이 다른 사람을 도울 수있는 당신의 해결책에 대해 더 구체적으로 설명한다면 그것은 좋을 것입니다. – ishanbakshi

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 프로 바이더로 MOXY을 지정하려면 사용자가 다음 항목으로 도메인 모델과 동일한 패키지에 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