【发布时间】:2017-06-06 20:18:50
【问题描述】:
我通过从 XML 文件中读取数据并使用 PHP 将数据推送到我的 MySQL 表中来学习一些关于 XML、MySQL 和 PHP 的知识,我遇到的问题是当 XML 字段为空时,它是返回错误:Notice: Trying to get property of non-object in C:\xampp\htdocs\Website\update-products.php on line 177。
如何检查每个对象的字段是否为空?
XML 代码
<?xml version="1.0"?>
<books>
<book isbn="978-1594489501">
<title></title>
<author>Khaled Hosseini</author>
<publisher>Riverhead Hardcover</publisher>
<amazon_price></amazon_price>
</book>
<book isbn="978-1594489587">
<title>The Brief Wondrous Life of Oscar Wao</title>
<author>Junot Diaz</author>
<publisher></publisher>
<amazon_price>14.97</amazon_price>
</book>
<book isbn="978-0545010221">
<title>Harry Potter and the Deathly Hallows</title>
<author>J. K. Rowling</author>
<publisher>Arthur A. Levine Books</publisher>
<amazon_price>19.24</amazon_price>
</book>
</books>```
部分 PHP 代码
$publisher = $xmlObject->item($i)->getElementsByTagName('publisher')->item(0)->childNodes->item(0)->nodeValue;
【问题讨论】: