2017-11-08 21 views
0

私はxmlファイルを解析していますが、タグ( ":g")に関するいくつかの問題を抱えています、私は情報、彼のコンテンツにアクセスできません、問題は私がカテゴリを取得しようとするときです複数のカテゴリー。すべてのgoogleタグを取得していますxml

のxml:私は、たとえば使用してカテゴリを取得してみてください

<item> 
     <g:id>4011700742288</g:id> 
     <title><![CDATA[4711 Acqua Colonia Blood Orange & Basil Eau de Cologne 170ml]]></title> 
     <link><![CDATA[https://url/asdasd.html]]></link> 
     <g:image_link><![CDATA[https://url/media/catalog/product/4/7/4711-acqua-colonia-blood-_2.jpg]]></g:image_link> 
     <g:price>34.86 EUR</g:price> 
     <g:product_type><![CDATA[Mulher]]></g:product_type> 
     <g:product_type><![CDATA[Homem]]></g:product_type> 
     <g:product_type><![CDATA[Unisexo]]></g:product_type> 
    </item> 

は:

$categories = $item->children('g', TRUE)->product_type; 

しかし、それは唯一の最初のカテゴリをもたらすには、カテゴリの残りの部分をgetingされていません。 上記のデータを取得する方法のコード例です。 例:

foreach($rss->channel->item as $item) { 
     $categories = $item->children('g', TRUE)->product_type; 


     // bringing in to array <content:encoded> items from SimpleXMLElement Object() 
     $content = xmlObjToArr($item->children('content', true)->encoded); 
      echo $categories . PHP_EOL; 
    return; 
} 


function xmlObjToArr($obj) { 
     $namespace = $obj->getDocNamespaces(true); 
     $namespace[NULL] = NULL; 

     $children = array(); 
     $attributes = array(); 
     $name = strtolower((string)$obj->getName()); 

     $text = trim((string)$obj); 
     if(strlen($text) <= 0) { 
      $text = NULL; 
     } 

     // get info for all namespaces 
     if(is_object($obj)) { 
      foreach($namespace as $ns=>$nsUrl) { 
       // atributes 
       $objAttributes = $obj->attributes($ns, true); 
       foreach($objAttributes as $attributeName => $attributeValue) { 
        $attribName = strtolower(trim((string)$attributeName)); 
        $attribVal = trim((string)$attributeValue); 
        if (!empty($ns)) { 
         $attribName = $ns . ':' . $attribName; 
        } 
        $attributes[$attribName] = $attribVal; 
       } 

       // children 
       $objChildren = $obj->children($ns, true); 
       foreach($objChildren as $childName=>$child) { 
        $childName = strtolower((string)$childName); 
        if(!empty($ns)) { 
         $childName = $ns.':'.$childName; 
        } 
        $children[$childName][] = xmlObjToArr($child); 
       } 
      } 
     } 

     return array(
      'name'=>$name, 
      'text'=>$text, 
      'attributes'=>$attributes, 
      'children'=>$children 
     ); 
    } 
+0

あなたが実際にコードで 'xmlObjToArr'関数の結果を使用していないので、表示すると、[編集]し、あなたの例から削除することができます。私の助言はあなたのコードからもそれを削除し、SimpleXMLオブジェクトを使用することを意図したものを使用することです。 – IMSoP

答えて

1

コードは正しいです。

$categories = $item->children('g', TRUE)->product_type; 

これは、あなたがすべての<g:product_type>の要素にアクセスすることができますオブジェクトへ$categoriesを設定します。

あなたが書くとき、あなたの問題は、次のとおりです。

echo $categories . PHP_EOL; 

これは、単一のXML要素のテキストの内容を表示します。 $categoriesは複数の要素のコレクションなので、SimpleXMLは最初の要素が必要であると推測します。

(string)は、テキストコンテンツを抽出し、echoによって暗示されており、[0]は、コレクション内の最初の項目を取得します
echo (string)$categories[0] . PHP_EOL; 

:つまり、それはと同等です。要素のコレクションをループ

は、リストが動作することを期待したい正確にどのように動作します - あなたはforeachを使用します。

foreach ($categories as $cat) { 
    echo $cat . PHP_EOL; 
} 
+0

ご協力ありがとうございました –

関連する問題