php - XML 2 mySQL 致命错误:调用成员函数 item()
问题描述
实际上我试图将 XML 导入 MySQL
for ($i=0; $i < $itemCount; $i++) {
$title = $xmlObject->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$link = $xmlObject->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$image_link = $xmlObject->item($i)->getElementsByTagName('image_link')->item(0)->childNodes->item(0)->nodeValue;
$price = $xmlObject->item($i)->getElementsByTagName('price')->item(0)->childNodes->item(0)->nodeValue;
$brand = $xmlObject->item($i)->getElementsByTagName('brand')->item(0)->childNodes->item(0)->nodeValue;
$availability = $xmlObject->item($i)->getElementsByTagName('availability')->item(0)->childNodes->item(0)->nodeValue;
$id = $xmlObject->item($i)->getElementsByTagName('id')->item(0)->childNodes->item(0)->nodeValue;
$product_type = $xmlObject->item($i)->getElementsByTagName('product_type')->item(0)->childNodes->item(0)->nodeValue;
$description = $xmlObject->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$sale_price = $xmlObject->item($i)->getElementsByTagName('sale_price')->item(0)->childNodes->item(0)->nodeValue;
添加最后一个对象后,Saleprice
我收到错误消息
致命错误:在 null 中调用成员函数 item()
我认为问题在于不是在每个项目中都设置了“销售价格”的价值
但我该如何解决它 - 如果设置了值 > 值将被导入 MySQL
提前致谢
XML 示例
<item>
<g:id>4</g:id>
<title>sadsadsdsadsadsadsadasd</title>
<description>dddddddd</description>
<g:product_type>aaaaaaaaa</g:product_type>
<link>https://www.xyz.de</link>
<g:image_link>https://www.xyz.de/600x600.jpg</g:image_link>
<g:condition>new</g:condition>
<g:availability>out of stock</g:availability>
<g:price>5,95</g:price>
<g:sale_price>4,99</g:sale_price>
<g:brand>asdasdsddsn</g:brand>
<g:gtin>137</g:gtin>
<g:mpn></g:mpn>
<g:shipping>
<g:country>DE</g:country>
<g:service>Standard</g:service>
<g:price>7,99</g:price>
</g:shipping>
<pubDate>Fri, 07 Dec 2018 12:10:02 CET</pubDate></item>
解决方案
您将访问列表的第一个节点而不检查列表是否有节点。
$sale_price =
$xmlObject // DOM node list
->item($i) // DOM node at index $i
->getElementsByTagName('sale_price') // list of'sale_price' descendant elements
->item(0) // first node in list - NULL if list is empty
->childNodes // child nodes list - this includes text nodes
->item(0) // first node in list - NULL if list is empty
->nodeValue; // node content
如果这里没有sale_price
元素(在您的上下文节点之一中),则会发生错误。因此,如果使用 DOM 方法,则必须检查每个级别以避免这种情况。
或者您开始使用 XPath:
$xml = <<<'XML'
<products>
<product>
<title>With sale_price</title>
<sale_price>42.00</sale_price>
</product>
<product>
<title>Without sale_price</title>
</product>
</products>
XML;
$document = new \DOMDocument();
$document->loadXML($xml);
$xpath = new \DOMXpath($document);
foreach($xpath->evaluate('//product') as $product) {
echo $xpath->evaluate('string(title)', $product), ': ';
echo $xpath->evaluate('string(sale_price)', $product), "\n";
}
输出:
With sale_price: 42.00
Without sale_price:
DOMXpath::evaluate()
//product
如果将节点列表强制转换到表达式中,则可以返回带有位置路径表达式的节点列表和标量值。string(title)
将所有子节点提取title
到一个列表中,并将第一个节点转换为一个字符串(返回其文本内容),如果找不到节点,它将返回一个空字符串。
推荐阅读
- sas - 尝试使用 where 语句创建二进制变量
- javascript - 异步/等待不适用于 AWS S3 Node.js 开发工具包
- javascript - Javascript 幻灯片如何使其更干燥
- sql-server - 我可以在 ssrs 报告中添加过滤器按钮吗?
- python - celery flask PicklingError,属性查找
在 flask_security.core 上失败 - android - 为特定节点设置 firebase 实时数据库规则
- typescript - 为什么我不能使用常量中的值作为自定义类型定义?
- php - 发送 URL 作为名称
- python - 尝试在 Python 2.7.6 中创建一个简单的 GUI,但 Tkinter 模块似乎不适用于我的版本
- javascript - 滚动替换