2011-07-01 25 views
1

このxmlをソープ関数で使用する配列に渡す方法。以下のようなネストされた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 
) 

便利なドキュメントへのリンク:

関連する問題