【发布时间】:2021-01-03 12:49:09
【问题描述】:
我确实有以下 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 ▶}
]
}
我想访问 entry 元素并对其进行 foreach。但是当我这样做时:
var_dump($xml->entry)
我只得到第一个元素
如果我这样做:
var_dump($xml->xpath('//entry'))
我得到一个空数组。
这是 xml 文件:http://www.telasxmetro.com/XMLData/googleshopping.xml
这是我的代码:
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.
}
【问题讨论】:
-
请向我们展示您的代码
-
我刚刚添加了一些代码,我唯一的问题是获取完整的数组,然后在其上使用 foreach。
-
所以试试
foreach ($xml->entry as $item) { var_dump($item); } -
由于您有一个默认命名空间 (
xmlns="http://www.w3.org/2005/Atom"),您必须注册它并将其用作 XPath 的一部分,有点像 stackoverflow.com/questions/21143846/… 或类似的东西。 -
@RiggsFolly 我试过了,我只得到了数组的第一个元素。