2011-09-14 65 views
2

我使用Sprint 3.0.5和附帶的jaxb編組器與REST服務進行通信。由不同公司提供的服務通過POST將XML發送給我的服務,並且我必須在我的Java對象中解組該XML並處理它們。JaxB使用大寫字母標記對XML進行解組

我遇到的問題是,XML標籤以大寫字母開頭(他們不會改變),因此JAXB編組人員不能解組對象。

的XML如下所示:

<Ssm_Packet> 
<Version>1</Version> 
<Protocol_ID>0</Protocol_ID> 
<Packet_Id>{84ca597c-05e2-4357-897c-892f428c35ce}</Packet_Id> 
<Priority>0</Priority> 
<Source_Address>1:11111111111111</Source_Address> 
<Destination_Address>2:LA3222222222222</Destination_Address> 
<Body>Some TExt</Body> 
<Billing /> 
</Ssm_Packet> 

我的JAXB定義的bean如下所示:

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


@XmlRootElement(name="Ssm_Packet") 
public class Ssm_Packet { 
@XmlElement(name="Version") 
private String version; 
private String protocol_ID; 
private String packet_Id; 
private String priority; 
private String source_Address; 
private String destination_Address; 
private String body; 
private String billing; 
/** 
* @return the version 
*/ 
public String getVers() { 
    return version; 
} 
/** 
* @param version the version to set 
*/ 
public void setVers(String Version) { 
    this.version = Version; 
} 
/** 
* @return the protocol_ID 
*/ 
public String getProtocol_ID() { 
    return protocol_ID; 
} 
/** 
* @param protocol_ID the protocol_ID to set 
*/ 
public void setProtocol_ID(String Protocol_ID) { 
    this.protocol_ID = Protocol_ID; 
} 
/** 
* @return the packet_Id 
*/ 
public String getPacket_Id() { 
    return packet_Id; 
} 
/** 
* @param packet_Id the packet_Id to set 
*/ 
public void setPacket_Id(String Packet_Id) { 
    this.packet_Id = Packet_Id; 
} 
/** 
* @return the priority 
*/ 
public String getPriority() { 
    return priority; 
} 
/** 
* @param priority the priority to set 
*/ 
public void setPriority(String Priority) { 
    this.priority = Priority; 
} 
/** 
* @return the source_Address 
*/ 
public String getSource_Address() { 
    return source_Address; 
} 
/** 
* @param source_Address the source_Address to set 
*/ 
public void setSource_Address(String Source_Address) { 
    this.source_Address = Source_Address; 
} 
/** 
* @return the destination_Address 
*/ 
public String getDestination_Address() { 
    return destination_Address; 
} 
/** 
* @param destination_Address the destination_Address to set 
*/ 
public void setDestination_Address(String Destination_Address) { 
    this.destination_Address = Destination_Address; 
} 
/** 
* @return the body 
*/ 
public String getBody() { 
    return body; 
} 
/** 
* @param body the body to set 
*/ 
public void setBody(String Body) { 
    this.body = Body; 
} 
/** 
* @return the billing 
*/ 
public String getBilling() { 
    return billing; 
} 
/** 
* @param billing the billing to set 
*/ 
public void setBilling(String Billing) { 
    this.billing = Billing; 
} 
} 

現在,如果我讓JAXB解組在該對象的XML,他不會填寫xml的值,除非xml標籤沒有大寫字母。

任何人都可以幫助我如何將這些值解組到我的bean?

THX

回答

3

可以使用@XmlElement批註指定對應於每個字段/屬性的元素名稱。

/** 
* @return the protocol_ID 
*/ 
@XmlElement(name="Protocol_ID") 
public String getProtocol_ID() { 
    return protocol_ID; 
} 

如果您想註解你的領域,而不是,那麼你就需要設置@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement(name="Ssm_Packet") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Ssm_Packet { 
    @XmlElement(name="Version") 
    private String version; 
} 

EclipseLink JAXB (MOXy)還包含一個擴展,你可以重寫標準JAXB算法將Java字段/屬性名稱XML名稱:

+0

Thx很多,這解決了我的問題。現在將檢查您提供的鏈接。 – Florian