开发者

How to Parse Nested XML to nested Array?

How to pass this xml into array to use in soap function. like

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

<OTA_HotelDestinationsRQ Version="1.0">
      <POS>
        <Source>
             <UniqueId Id="username:password"开发者_如何转开发 />           
        </Source>
      </POS>
      <DestinationInformation LanguageCode="EN" />
</OTA_HotelDestinationsRQ>


You can use code like this:

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;
}

And the result:

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

                )

        )

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

    [text] => text
)

Useful documentation links:

  • DOMNode
  • DOMDocument
  • DOMElement
  • DOMNameNodeMap
  • DOMCharacterData
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜