2017-04-10 23 views
1

とXMLをループ:は、私はこのようなXMLカール応答を持つ名前空間PHP

<ns2:categorys xmlns:ns2="http://skt.tmall.business.openapi.spring.service.client.domain"> 
    <ns2:category> 
     <depth>1</depth> 
     <dispengnm>Women Clothing</dispengnm> 
     <dispnm>Pakaian Wanita</dispnm> 
     <dispno>1</dispno> 
     <parentdispno>0</parentdispno> 
    </ns2:category> 
    <ns2:category> 
     <depth>2</depth> 
     <dispengnm>Dresses &amp; Suits</dispengnm> 
     <dispnm>Gaun &amp; Terusan</dispnm> 
     <dispno>2</dispno> 
     <parentdispno>1</parentdispno> 
    </ns2:category> 
</ns2:categorys> 

私はこのコードを使用して<ns2:category>内のデータを取得しようとしました:

$return = curl_exec($ch); 

$return= simplexml_load_string($return); 
$categories = $return->children('ns2', true); 
echo "<pre>"; 
var_dump(count($categories)); 
foreach ($categories as $category) { 
    print_r($category->depth); 
} 

しかし、私が得たすべてがあります:あなたがgetNamespaces befor trueに設定する必要があり

int(2) 
SimpleXMLElement Object 
(
) 
SimpleXMLElement Object 
(
) 

答えて

1

電子データをループし、あなたはそれがデータを取得し、オブジェクト変数

$return = simplexml_load_string($return); 
$ns = $return->getNamespaces(true); 

foreach ($return->children($ns['ns2'])->category as $key) { 
    $data = $key->children(); 
    echo $data->depth; 
} 
+0

内側に包まれますように、子どもたち関数を呼び出す必要がループ内おかげで、それは魔法のように動作男 –

関連する問題