[英]DOMDocument parsing (php)
I am parsing an xml file from an API which I have converted into a DOMDocument in php. 我正在从API中解析一个xml文件,而我已将其转换为php中的DOMDocument。 This is mostly fine but one problem I have is when I do this:
这通常很好,但是我遇到的一个问题是何时执行此操作:
$feeditem->getElementsByTagName('extra');
as part of a forall statment and the element extra doesn't exist in one of the feeditems I am iterating through in the forall condition then I get an error. 作为forall语句的一部分,在forall条件下我要遍历的一个馈送项中不存在extra元素,然后出现错误。
I tried this: 我尝试了这个:
if (!empty($feeditem->getElementsByTagName('extra'))){
$extratag = $feeditem->getElementsByTagName('extra');
$extraname = $extratag->item(0)->getAttribute('name');
echo $extraname
}
But I get the error 但是我得到了错误
getAttribute() on a non-object
Note: When the 'extra' element is contained in every feeditem then the code runs perfect. 注意:当每个供稿项中都包含“额外”元素时,代码将完美运行。 it's just when one of the feed items doesn't contain an 'extra' element I get the error.
只是其中一个供稿项中不包含“额外”元素时,我才收到错误消息。
Try to do use length
property of DOMNodeList
: 尝试使用
DOMNodeList
length
属性:
$nodes = $feeditem->getElementsByTagName('extra');
if ($nodes->length > 0) {
$extraname = $extratag->item(0)->getAttribute('name');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.