doubian0284 2017-06-06 20:18
浏览 233

XML到MySQL检查XML字段是否为空

I'm learning a little about XML, MySQL and PHP by reading data from and XML file and using PHP to push that data to my MySQL table, the problem I'm running into is when an XML field is empty, it's returning an error: Notice: Trying to get property of non-object in C:\xampp\htdocs\Website\update-products.php on line 177.

How can I do a check to see if the field is empty for each object?

XML Code

<?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>```

Part of PHP Code

$publisher = $xmlObject->item($i)->getElementsByTagName('publisher')->item(0)->childNodes->item(0)->nodeValue;
  • 写回答

1条回答 默认 最新

  • douyong1285 2017-06-07 23:52
    关注

    It's hard to say exactly without seeing the code around line 177. With that said, you'll need to check for the existence of potentially missing nodes before operating on them.

    if(isset($document->node->child)){
       doStuff($document->node->child);
    }
    

    This way you can skip missing nodes in the document without hitting an error.

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题