dsdzvprlp51692469 2017-02-24 11:38
浏览 25

检查孩子是否存在? - SimpleXML(PHP)

I have different XML files where I renamed for each XML file all individual tags, so that every XML file has the same tag name. That was easy because the function was customized for the XML file.

But instand of writing 7 new functions for each XML file now I want to check if a XML file has a specidifed child or not. Because if I want to say:

foreach ($items as $item) {
$node = dom_import_simplexml($item);
$title = $node->getElementsByTagName('title')->item(0)->textContent;
$price = $node->getElementsByTagName('price')->item(0)->textContent;
$url = $node->getElementsByTagName('url')->item(0)->textContent;
$publisher = $node->getElementsByTagName('publisher')->item(0)->textContent;
$category = $node->getElementsByTagName('category')->item(0)->textContent;
$platform = $node->getElementsByTagName('platform')->item(0)->textContent;
}

I get sometimes: PHP Notice: Trying to get property of non-object in ...

For example. Two different XML sheets. One contains publisher, category and platform, the other not:

XML 1:

<products>
  <product>
    <desc>This is a Test</desc>
    <price>11.69</price>
    <price_base>12.99</price_base>
    <publisher>Stackoverflow</publisher>
    <category>PHP</category>
    </packshot>
    <title>Check if child exists? - SimpleXML (PHP)</title>
    <url>http://stackoverflow.com/questions/ask</url>
  </product>
</products>

XML 2:

<products>
  <product>
   <image></image>
    <title>Questions</title>
    <price>23,90</price>
    <url>google.de/url>
    <platform>Stackoverflow</platform>
  </product>
</products>

You see, sometimes one XML file contains publisher, category and platform but sometimes not. But it could also be that not every node of a XML file contains all attributes like in the first!

So I need to check for every node of a XML file individual if the node is containing publisher, category or/and platform.

How can I do that with SimpleXML? I thought about switch case but at first I need to check which childs are contained in every node.

EDIT: Maybe I found a solution. Is that a solution or not?

if($node->getElementsByTagName('platform')->item(0)){
        echo $node->getElementsByTagName('platform')->item(0)->textContent . "
";
    }

Greetings and Thank You!

  • 写回答

2条回答 默认 最新

  • douyuan1049 2017-02-24 11:58
    关注

    One way to rome... (working example)

    $xml = "<products>
      <product>
        <desc>This is a Test</desc>
        <price>11.69</price>
        <price_base>12.99</price_base>
        <publisher>Stackoverflow</publisher>
        <category>PHP</category>
        <title>Check if child exists? - SimpleXML (PHP)</title>
        <url>http://stackoverflow.com/questions/ask</url>
      </product>
    </products>";
    $xml = simplexml_load_string($xml);
    #set fields to look for
    foreach(['desc','title','price','publisher','category','platform','image','whatever'] as $path){
           #get the first node
           $result = $xml->xpath("product/{$path}[1]");
           #validate and set
           $coll[$path] = $result?(string)$result[0]:null;  
           #if you need here a local variable do (2 x $)
           ${$path} = $coll[$path];
    }
    #here i do array_filter() to remove all NULL entries
    print_r(array_filter($coll));
    #if local variables needed do
    extract($coll);#this creates $desc, $price
    

    Note </packshot> is an invalid node, removed here.

    xpath syntax https://www.w3schools.com/xmL/xpath_syntax.asp

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)