2013-09-05 2 views
0

이러한 XML 조각을 반복하는 올바른 방법은 무엇입니까?PHP에서 일련의 XML 노드를 반복하는 올바른 방법

안녕하세요.

나는 통합 할 클라이언트를 가져 오는 웹 서비스가 있습니다.

werbservice는 하나의 클라이언트를 반환하는 경우

는 결과는 다음과 같다 :

<ClientsToIntegrateResult> 
    <Client> 
     <Id>1</Id> 
     <Name>John</Name> 
    </Client> 
    <Client> 
     <Id>2</Id> 
     <Name>Peter</Name> 
    </Client> 
</ClientsToIntegrateResult> 

그리고 내가 가진 다음 웹 서비스가 하나 이상의 클라이언트를 반환

<ClientsToIntegrateResult> 
    <Client> 
     <Id>1</Id> 
     <Name>John</Name> 
    </Client> 
</ClientsToIntegrateResult> 

경우, 결과는 다음과 같이이다 이 PHP 코드 조각은 webService에서 결과를 읽습니다. $ myWSResponse는 myWebService의 인스턴스입니다.

webservice가 하나의 클라이언트 만 반환하면 내 코드가 작동하지 않습니다. 두 개 이상의 클라이언트가있을 때 은 배열이되므로 내 foreach는 제대로 작동합니다. 그러나 webService가 하나의 클라이언트 만 반환하면 $clients->Client은 노드 자체가되고 내 코드는 오류를 발생시킵니다.

해결하려면 어떻게해야합니까?

저는 PHP 개발에 익숙하지 않지만 .NET 개발에 익숙합니다. 어떤 도움이라도 소중한 것입니다.

+1

넓은 범위 - PHP에는 XML을 표현하고 처리하는 무수한 객체가 있습니다. 우리는 $ myWSResponse 객체가 어떤 종류인지 알 필요가 있습니다 (var_dump (get_class ($ myWSResponse)))가 당신을 도울 수 있습니다. –

+0

@AlanStorm'var_dump (get_class ($ myWSResponse))'의 결과는'string (8)입니다. "stdClass"' –

답변

0
if(count($clients->Client) > 1){ 

foreach ($clients->Client as $client) 
{ 
    print_r($client); //Print the client node and its properties if more than one client, and prints each property of client node if only one client. 
    print_r($client->Id); //Give me error if only one client, because Id will not exist. 
} 

}else { 
echo $clients->Client['id']; //and other child attributes 
} 
관련 문제