2012-05-10 85 views
2

在Savon中如何根據item_id中元素的數量創建一個標籤數組?任何其他方式呢?Ruby - Rails - Savon結構SOAP XML主體

item_id = "abc, def, xyz" 
n = item_id.split(,).length 

    #ItemList shall be of n times by creating ItemList for each 
soap.body = { 
    'Body' => { 
       'ItemList' => { 
    'ItemId' => "abc" 
       } 
       'ItemList' => { 
    'ItemId' => "def" 
       } 
       'ItemList' => { 
    'ItemId' => "xyz" 
       } 
      } 
    } 

編輯:下面是實際預期的XML的一個樣本,Body應有多個ItemList價值標籤爲每個ItemIdItemId長度可以是任何東西。

item_id = "abc, def, xyz" #split this and make xml for each item. 

<Body> 
<ItemList> 
<ItemId>abc</ItemId> #abc 
<TimeStamp> 
    <Time>04:57:40</Time> 
    <TimeZone>EST</TimeZone> 
    <Date>2012-05-12</Date> 
</TimeStamp> 
<ContactInfo> 
    <Phone>888-888-8880</Phone> 
    <Email>[email protected]</Email> 
    <Name>name1</Name> 
</ContactInfo> 
</ItemList> 

<ItemList> 
<ItemId>def</ItemId> #def 
<TimeStamp> 
    <Time>04:27:40</Time> 
    <TimeZone>EST</TimeZone> 
    <Date>2012-05-12</Date> 
</TimeStamp> 
<ContactInfo> 
    <Phone>888-888-8881</Phone> 
    <Email>[email protected]</Email> 
    <Name>name2</Name> 
</ContactInfo> 
</ItemList> 

<ItemList> 
<ItemId>xyz</ItemId> #xyz 
<TimeStamp> 
    <Time>04:37:40</Time> 
    <TimeZone>EST</TimeZone> 
    <Date>2012-05-12</Date> 
</TimeStamp> 
<ContactInfo> 
    <Phone>888-888-8882</Phone> 
    <Email>[email protected]</Email> 
    <Name>name3</Name> 
</ContactInfo> 
</ItemList> 
</Body> 
+0

您在XML中的預期結果是什麼? XML如何看起來像使用上述語句創建的? –

+0

@SteffenRoller - 嗨Steffen。我更新了我的問題。基本上我有一個字符串/或我想要爲其生成xml的數組。但分割值本身只是xml中的一個內部標籤。我必須爲每個項目創建多個外部標籤並在其中添加其他數據。 –

回答

1

我會使用這樣的構建器。然後在Savon中使用生成的xml字符串作爲xml.body。

require 'builder' 

itemliste = [ 
    { 'ItemId' => 'abc', 
    'TimeStamp' => 
     { 'Time' => '04:57:40', 
      'TimeZone' => 'EST', 
      'Date' => '2012-05-12'}, 
    'ContactInfo' => 
     { 'Phone' => '888-888-8880', 
      'Email' => '[email protected]here.com', 
      'Name' => 'John Doe' } }, 
    { 'ItemId' => 'def', 
    'TimeStamp' => 
     { 'Time' => '04:27:40', 
      'TimeZone' => 'EST', 
      'Date' => '2012-05-12'}, 
    'ContactInfo' => 
     { 'Phone' => '888-888-8881', 
      'Email' => '[email protected]', 
      'Name' => 'Jane Doe' } }, 
    { 'ItemId' => 'ghi', 
    'TimeStamp' => 
     { 'Time' => '04:17:40', 
      'TimeZone' => 'EST', 
      'Date' => '2012-05-12'}, 
    'ContactInfo' => 
     { 'Phone' => '888-888-8882', 
      'Email' => '[email protected]', 
      'Name' => 'Jane Three' } }, 
    { 'ItemId' => 'xyz', 
    'TimeStamp' => 
     { 'Time' => '04:07:40', 
      'TimeZone' => 'EST', 
      'Date' => '2012-05-12'}, 
    'ContactInfo' => 
     { 'Phone' => '888-888-8883', 
      'Email' => '[email protected]', 
      'Name' => 'Joe Sixpack' } } 
] 

item_id = "abc,def,xyz" 
keys = item_id.split(',') 
xml_body = "" 
xml = Builder::XmlMarkup.new(:target => xml_body, :indent => 2) 
xml.Body do 
    itemliste.each do |item| 
    if item_id.include? item['ItemId'] 
     xml.ItemList do 
     xml.ItemId item['ItemId'] 
     xml.TimeStamp do 
      xml.Time item['TimeStamp']['Time'] 
      xml.TimeZone item['TimeStamp']['TimeZone'] 
      xml.Date item['TimeStamp']['Date'] 
     end 
     xml.ContactInfo do 
      xml.Phone item['ContactInfo']['Phone'] 
      xml.Name item['ContactInfo']['Name'] 
      xml.Email item['ContactInfo']['Email'] 
     end 
     end 
    end 
    end 
end 
puts xml_body 
+0

再次感謝您幫助steffen! – rubiii