2013-10-31 51 views
0

我把這個xml格式作爲不同http請求的輸出。將類的類型綁定到元素的動態方式

<transaction> 
<respmsg>...</respmsg> 
<respcode>0000</respcode> 
<status>success</status> 
</transaction> 

下面是我的交易類表示

@root 
class Transaction { 
    @Element 
    public ResponseMessage respmsg; 
    @Element 
    public int respcode; 
    @Element 
    public String status; 
} 

的 「respmsg」 屬性

abstract class ResponseMessage { 

} 

的抽象類,表示這是相當容易的映射到一個類的對象。然而「respmsg」是用於登錄請求每個請求 不同的respmsg可能是

<respmsg> 
    <firstname>John</firstname> 
    <lastname>Doe</lastname> 
    <mobilephone>1234</mobilephone> 
    <email>[email protected]</email> 
</respmsg> 


class LoginResponseMessage extends ResponseMessage { 
    @Element 
    public String firstname; 

    @Element 
    public String lastname; 

    @Element 
    public String mobilephone; 

    @Element 
    public String email; 
} 

和位置要求可能是

<respmsg> 
    <address1>121 121 UAE Bldg, dubai</address1> 
    <address2/> 
    <city>Dubai</city> 
    <postalcode>121</postalcode> 
    <region>dubai</region> 
</respmsg> 

class LocationResponseMessage extends ResponseMessage { 
    @Element 
    public String address1; 

    @Element 
    public String address2; 

    @Element 
    public String postalcode; 

    @Element 
    public String region; 
} 

如何設計我的類,以便它知道什麼類型在解序列化過程中用於respmsg的類?

String xmlData = retrieve(urls[0]); //retrieve fetches the string xml from the http request 
Serializer serializer = new Persister(); 
Reader reader = new StringReader(xmlData); 
Transaction t = serializer.read(Transaction.class, reader, false); 
+0

什麼添加屬性到''說明標籤哪種類型是? – azz

+0

例如:'' – azz

+0

Hi Der Flatulaor感謝您的評論。是的,我確實從String xmlData向插入了一個屬性。 String xmlData = retrieve(urls [0]); int respmsgIndex = xmlData.indexOf(「」); xmlData = xmlData.replaceFirst(「」,「」); 謝謝你的幫助。如果可以的話,我會將你的評論標記爲答案。非常感激! – Lian

回答

0

按照要求,發佈我的評論作爲一個答案:


有關添加屬性的<respmsg>標籤,說明該類型是什麼?

例如:

​​
相關問題