我正在使用ECCP協議,以便將我的CRM與Elastix呼叫中心模塊集成。該協議使用定義的XML結構如下:JAX-B定製的XML輸出
<request id="1">
<request_type> <!-- this will be mapped to the Java request class -->
<attributes>
</attributes>
</request_type>
</request>
和
<response id="1">
<response_type> <!-- this will be mapped to the Java response class -->
<attributes>
</attributes>
</response_type>
</response>
我使用JAX-B到XML映射到Java類,但問題是,我必須把JAX- B在<request></request>
XML中爲每個請求生成XML,並在每個響應中從<response></response>
中提取內容,因爲ECCP協議定義每個請求和響應都需要嵌套到它們各自的元素。
下面是我使用這樣做的代碼:
document = createDocument();
Element requestWrapper = document.createElement("request");
requestWrapper.setAttribute("id", String.valueOf(wrapped.getId()));
document.appendChild(requestWrapper);
JAXBContext jc = JAXBContext.newInstance(wrapped.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(wrapped, requestWrapper);
例示:
一個ECCP的協議操作的是JAX-B-映射到這樣一類(getter和setter省略):
@XmlRootElement(name = "loginagent")
@XmlAccessorType(XmlAccessType.FIELD)
public class EccpLoginAgentRequest implements IEccpRequest {
@XmlElement(name = "agent_number")
private String agentNumber;
@XmlElement(name = "password")
private String password;
}
和JAX-B輸出以下:
<loginagent>
<agent_number>username</agent_number>
<password>password</password>
</loginagent>
但什麼ECCP的協議要求是:
<request id="1"> <!-- id is an auto-increment number to identify the request -->
<loginagent>
<username>username</username>
<password>password</password>
</loginagent>
</request>
的問題是:是否有任何其他方式在任何其他更好的方式來實現? 謝謝。
好吧,我看了看在@XmlSeeAlso註釋文檔,但我會提高我的問題,以更好地解釋這個問題。 – Jaumzera
如果您可以再次檢查問題,我會很高興。提前致謝。 – Jaumzera
再次感謝@reallol,但它只會在我發送一個EccpLoginAgentRequest對象時起作用,而我真正需要的是能夠從任何擴展EccpRequest的類發送對象。 – Jaumzera