2017-11-08 4 views
0

Im는 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

: 당신이 foreach를 사용 - 요소의 컬렉션을 통해 반복

당신이 목록 작업을 기대 정확히 어떻게 작동 당신이 보여 주면, 당신이 [편집]하고 예제에서 제거 할 수 있습니다. 내 충고는 코드에서 코드를 제거하고 SimpleXML 객체를 사용하기위한 목적으로 사용하는 것입니다. – IMSoP

답변

1

올바른 코드입니다.

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

이것은 당신이 모든 <g:product_type> 요소에 액세스 할 수 있습니다 객체에 $categories를 설정합니다. 당신이 쓸 때

귀하의 문제는 다음과 같습니다

echo $categories . PHP_EOL; 

하나의 XML 요소의 텍스트 내용이 표시됩니다. $categories은 여러 요소 모음이므로 SimpleXML은 첫 번째 요소를 원한다고 추측합니다.

echo (string)$categories[0] . PHP_EOL; 
(string)이 텍스트 내용을 추출하고 echo에 의해 암시

[0] 컬렉션의 첫 번째 항목을 가져옵니다에 즉, 해당합니다. 실제로 코드에서`xmlObjToArr` 함수의 결과를 사용하지 않는 때문에

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

도움과 명확한 설명에 감사드립니다. –

관련 문제