2011-07-01 142 views
1

如何將此xml傳遞到數組以便在soap函數中使用。像如何解析嵌套的XML嵌套數組?

$res=$client->OTA_HotelDestinationsRQ($myarray); 

<OTA_HotelDestinationsRQ Version="1.0"> 
     <POS> 
     <Source> 
      <UniqueId Id="username:password" />   
     </Source> 
     </POS> 
     <DestinationInformation LanguageCode="EN" /> 
</OTA_HotelDestinationsRQ> 
+0

爲什麼你必須有一個數組?使用DOM或SimpleXml提供的樹層次結構有什麼問題?你搜索了哪些[現有問題提問相同](http://stackoverflow.com/search?q=xml+to+array+php)? – Gordon

+0

我想傳遞數組作爲肥皂請求 – Hearaman

+0

當我使用$ res = $ client - > _ call(「OTA_HotelDestinationsRQ」,array($ xml))這樣的代碼時,我得到了響應;如何在該OTA_HotelDestinationsRQ函數中傳遞數據數組 – Hearaman

回答

0

您可以使用這樣的代碼:

function process(DOMNode $node){ 
    // Special handling for character data 
    if($node instanceof DOMCharacterData){ 
     return $node->data; 
    } 

    // Has only text inside: 
    if(($node->childNodes->length == 1) && ($node->childNodes->item(0)->nodeName == '#text') && ($node->childNodes->item(0) instanceof DOMCharacterData)){ 
     return $node->childNodes->item(0)->data; 
    } 

    // Get all attributes 
    $result = array(); 
    foreach($node->attributes as $item){ 
     $result[ $item->name] = $item->value; 
    } 

    // Go trough all child nodes 
    foreach($node->childNodes as $sub){ 
     $result[$sub->nodeName] = process($sub); 
    } 

    return $result; 
} 

而結果:

Array 
(
    [Version] => 1.0 
    [POS] => Array 
     (
      [Source] => Array 
       (
        [UniqueId] => Array 
         (
          [Id] => username:password 
         ) 

       ) 

     ) 

    [DestinationInformation] => Array 
     (
      [LanguageCode] => EN 
     ) 

    [text] => text 
) 

有用的文檔鏈接: