PHP simplexml节点不再存在:如何获取属性值


php simplexml node no longer exists : how to get attribute value?

在浪费了6个多小时后,我把我的问题贴在这里。

我试图得到simplexmlelement属性值:

我的var_dump值:

object(SimpleXMLElement)#5 (1) {
 ["@attributes"]=>
 array(3) {
 ["type"]=>
string(4) "Rich"
["template"]=>
string(44) "EntityContainer.HeroGeneric_8_1_RTM-7814aaaa"
["disambiguationId"]=>
string(36) "85fa63d3-9596-adb9-b4eb-502273d84f56"
 }
}

我想获得["type"]值"Rich"。然而,我不能得到。我已经看到了很多的答案和代码的例子之前张贴在这里,但他们没有帮助。实际上我第一次尝试使用simplexmlelement [Advance code]

My php code

$xml = simplexml_load_file($url);
$xml2 = $xml->channel->item;
foreach ($xml2 as $out_ns) 
{ 
$ns = $out_ns->getNamespaces(true); 
$child = $out_ns->children($ns['win']);
var_dump($child); // Value is written above simple xml object
print_r((string) $child->attributes());
} 
我想知道我做错了什么。我想实现type和disambiguationId属性值。 错误:

警告: SimpleXMLElement::__toString(): Node不再存在于E:'xampp'htdocs'ring'dom.php on line 15

请检查我的代码帮助我。

你必须检查元素是否存在:

if($child &&  $child->attributes()) {
   print_r($child->attributes());
}