2013-02-26 64 views
1

的XML結構看起來像following.I想ID傳遞name="hotelId"value="00000000003054D8"和id name="offerId" value="74"Jsoup的Connectdata()方法。我想通過在地圖的屬性值Jsoup Connect.data()方法

<GetBookingIdsRequest token="f6ERmpwxbZ4ysUgCHB9mlSPcd9rf5DVB39C--yLbNSdG" sid="TVd8DY5OQi2vf82h" xmlns="http://www.travelfusion.com/xml/api/simple"> 
     <id name="hotelId" value="00000000003054D8"/> 
     <id name="offerId" value="74"/> 
</GetBookingIdsRequest> 

如何通過相同的?

回答

0

這裏有一個例子,你是如何得到的值:

Document doc = ... 

Elements request = doc.select("getbookingidsrequest"); 


for(Element element : request) 
{ 
    final String hotelId = element.select("id[name=hotelId]").first().attr("value"); 
    final String offerId = element.select("id[name=offerId]").first().attr("value"); 

    System.out.println(hotelId); 
    System.out.println(offerId); 
} 

輸出:

00000000003054D8 
74 

如果你只有一個GetBookingIdsRequest - 標籤,你可以使用first()方法,而不是for - 迴路:

Document doc = ... 

Element request = doc.select("getbookingidsrequest").first(); 

final String hotelId = request.select("id[name=hotelId]").first().attr("value"); 
final String offerId = request.select("id[name=offerId]").first().attr("value"); 

System.out.println(hotelId); 
System.out.println(offerId); 
+0

嘿的答覆感謝...但我的要求是小different..I知道如何獲得values.But我想設置在Jsoup.I值附上我code..It的有關如何設置值。以下是代碼片段。 – user2109823 2013-02-26 10:34:19

+0

< id name =「offerId」value =「4」/> user2109823 2013-02-26 10:38:02

0

您可以將以下內容作爲Connection對象的數據映射的一部分。

map.put("id[0].name", "hotelId"); 
map.put("id[0].value", hotel_Id); 
map.put("id[1].name", "offerId"); 
map.put("id[1].value", offer_Id);