0

I do have the following SimpleXMLElement:

SimpleXMLElement {#812 ▼
  +"title": "Telas x Metro"
  +"link": SimpleXMLElement {#876 ▶}
  +"updated": "2020-09-16T15:36:53Z"
  +"author": SimpleXMLElement {#883 ▶}
  +"id": "/googleshopping.xml"
  +"entry": array:4 [▼
    0 => SimpleXMLElement {#881 ▶}
    1 => SimpleXMLElement {#895 ▶}
    2 => SimpleXMLElement {#893 ▶}
    3 => SimpleXMLElement {#873 ▶}
  ]
}

And I would like to access to the entry element and the do a foreach on it. but when I do this:

var_dump($xml->entry)

I only get first element

and if i do this:

var_dump($xml->xpath('//entry'))

I get a empty array.

this is the xml file: http://www.telasxmetro.com/XMLData/googleshopping.xml

This is my code:

xml = simplexml_load_file(
 's3://'.env('AWS_BUCKET').'/'.$path,//this is the same xml.
 'SimpleXMLElement',
 LIBXML_NOCDATA
);

foreach ($xml->xpath('//entry') as $item) {
//empty array, so dont enter the loop.
}
Oswaldo C.
  • 99
  • 7
  • Please show us your code – RiggsFolly Sep 16 '20 at 16:25
  • I just added some code, my only problem is to get the full array, then using a foreach on it. – Oswaldo C. Sep 16 '20 at 16:33
  • so try `foreach ($xml->entry as $item) { var_dump($item); }` – RiggsFolly Sep 16 '20 at 16:39
  • As you have a default namespace (`xmlns="http://www.w3.org/2005/Atom"`) you will have to register it and use that as part of your XPath, sort of dupe of https://stackoverflow.com/questions/21143846/xpath-in-simplexml-for-default-namespaces-without-needing-prefixes or something similar. – Nigel Ren Sep 16 '20 at 16:42
  • @RiggsFolly I tried and I only get the first element of the array. – Oswaldo C. Sep 16 '20 at 16:45
  • Yes, first time round the loop you get Occurance1 and then Occ2 etc etc – RiggsFolly Sep 16 '20 at 16:52
  • @RiggsFolly i know, but I did var_dump($xml->entry) just before the loop and I got only the first element. – Oswaldo C. Sep 16 '20 at 16:58
  • Please [edit] your question to include a self-contained XML sample, and the actual code you're having problems with, along with the exact output you're getting and the exact output your expecting (in other words, a [mcve]). – IMSoP Sep 21 '20 at 09:00

1 Answers1

0

This works for me

$xml = simplexml_load_file('http://www.telasxmetro.com/XMLData/googleshopping.xml');

echo count($xml->entry) . ' Occurances' . PHP_EOL;

foreach ($xml->entry as $item) {
    echo $item->title . PHP_EOL;
}

Output is

818 Occurances

Lona estampada Hojas Tropicales Verde y Rosa
Lienzo Rayado Rojo
Lienzo Rayado Negro
Lienzo Rayado Azul
Lienzo Crudo 1,60 mt. de ancho.
Lona estampada Flores Turquesa Fondo Blanco
Lona estampada Flores Negras Fondo Blanco

etc etc 
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149