PHP检查XML中是否存在元素

时间:2016-05-11 11:24:09

标签: php mysql xml csv

我尝试读取XML文件,在表格中显示数据并将所需数据写入MySQL数据库,以便以后可以将数据提取为CSV。

在XML中是一组全部具有相同名称的数据。

每种产品都有不同数量的 <道具> (最多20)我想检查一个元素 <道具> 退出与否。现在我得到所有数据'来自道具。

我很难解释所以我将展示具有特定数据的嵌套元素。 编辑:现在是产品的完整数据。

        <?xml version="1.0" encoding="UTF-8"?>
            <products>
              <product>
                <id>1</id>
                <artnr>3009000555</artnr>
                <title>product</title>
                <description>Some long text</description>
                <brand>
                  <id>458</id>
                  <title>Brand</title>
                </brand>
                <date>11-05-2015</date>
                <modifydate>07-05-2016</modifydate>
                <variants>
                  <variant>
                    <id>35021</id>
                    <type></type>
                    <subartnr>3009000555</subartnr>
                    <ean>85252000555</ean>
                    <stock>Y</stock>
                  </variant>
                </variants>
                <price>
                  <currency>EUR</currency>
                  <b2b>9.39</b2b>
                  <b2c>16.95</b2c>
                  <vatnl>21</vatnl>
                  <vatde>19</vatde>
                  <discount>N</discount>
                </price>
                <categories>
                  <category>
                    <cat>
                      <id>1</id>
                      <title>Maincat</title>
                    </cat>
                    <cat>
                      <id>182</id>
                      <title>Subcat</title>
                    </cat>
                  </category>
                </categories>
                <measures>
                  <length>4.00</length>
                  <weight>243</weight>
                  <packing>10x9x10</packing>
                </measures>
                <properties>
                  <prop>
                    <propid>25</propid>
                    <property>Farbe</property>
                    <valueid>208</valueid>
                    <value>Weiß</value>
                  </prop>
                  <prop>
                    <propid>41</propid>
                    <property>Geeignet für</property>
                    <valueid>229</valueid>
                    <value>Unisex</value>
                  </prop>
                  <prop>
                    <propid>43</propid>
                    <property>Verpackung</property>
                    <valueid>236</valueid>
                    <value>Einzelhandelverpackung (Karton)</value>
                  </prop>
                  <prop>
                    <propid>54</propid>
                    <property>Gesamturteil</property>
                    <valueid>289</valueid>
                    <value>5</value>
                  </prop>
                  <prop>
                    <propid>70</propid>
                    <property>Garantie</property>
                    <valueid>326</valueid>
                    <value>Ja</value>
                  </prop>
                  <prop>
                    <propid>71</propid>
                    <property>Art der Garantie</property>
                    <valueid>328</valueid>
                    <value>Good Warranty Service</value>
                  </prop>
                </properties>
                <material>
                  <id>101</id>
                  <title>Keramik</title>
                </material>
                <popularity>3</popularity>
                <pics>
                  <pic>3009000022.jpg</pic>
                  <pic>3009000022_2.jpg</pic>
                </pics>
              </product>
              <product>

这是我现在所拥有的,但正如我之前所说,我也将不存在的道具作为空数据。 例如 :: 7, 编辑。现在我的整个代码。该产品仅有7 <道具&GT;其他有9或者15或者什么。所以我只需要展示现有的道具。但即使它们不存在,我也会从prop [0]到prop [19]得到所有。

        <html>
      <table id="mytable">
        <thead>
          <tr>        
            <th>Artnr</th>
            <th>Name</th>
            <th>Beschreibung</th>
            <th>Hersteller</th>
            <th>EAN</th>
            <th>Vorrat</th>
            <th>Preis EK</th>
            <th>Preis VK</th>
            <th>Sonderangebot</th>   
            <th>Kategorie</th>
            <th>Eigenschaften</th>
            <th>Bild URL</th>       
          </tr>
        </thead>
        <tbody>

     <?php

      $url = ('http://somdom.com/test1.xml');
      $xml = simplexml_load_file( urlencode($url), null, true);
      $kommaleerz = ', ';
      $doppelpunkt = ':';
      $bildaddy = 'http://somedom.com/img/product_pic/500/';


       foreach ( $xml->product as $prods ) :?>
          <tr>        
            <td><?php echo $prods->artnr; ?></td>
            <td><?php echo $prods->title; ?></td>
            <td><?php echo $prods->description; ?></td>
            <td><?php echo $prods->brand->title; ?></td>
            <td><?php echo $prods->variants->variant->ean; ?></td>
            <td><?php echo $prods->variants->variant->stock; ?></td>
            <td><?php echo $prods->price->b2b; ?></td>
            <td><?php echo $prods->price->b2c; ?></td>
            <td><?php echo $prods->price->discount; ?></td>
            <td><?php echo $prods->categories->category->cat[1]->title; ?></td>       
            <td><?php if (isset($prods->properties->prop)) {echo $prods->properties->prop[0]->property . $doppelpunkt . $prods->properties->prop[0]->value .$doppelpunkt .'1' .$kommaleerz 
                          .$prods->properties->prop[1]->property . $doppelpunkt . $prods->properties->prop[1]->value .$doppelpunkt .'2' .$kommaleerz
                          .$prods->properties->prop[2]->property . $doppelpunkt . $prods->properties->prop[2]->value .$doppelpunkt .'3' .$kommaleerz
                          .$prods->properties->prop[3]->property . $doppelpunkt . $prods->properties->prop[3]->value .$doppelpunkt .'4' .$kommaleerz        
                          .$prods->properties->prop[4]->property . $doppelpunkt . $prods->properties->prop[4]->value .$doppelpunkt .'5' .$kommaleerz        
                          .$prods->properties->prop[5]->property . $doppelpunkt . $prods->properties->prop[5]->value .$doppelpunkt .'6' .$kommaleerz         
                          .$prods->properties->prop[6]->property . $doppelpunkt . $prods->properties->prop[6]->value .$doppelpunkt .'7' .$kommaleerz
                          .$prods->properties->prop[7]->property . $doppelpunkt . $prods->properties->prop[7]->value .$doppelpunkt .'8' .$kommaleerz
                          .$prods->properties->prop[8]->property . $doppelpunkt . $prods->properties->prop[8]->value .$doppelpunkt .'9' .$kommaleerz
                          .$prods->properties->prop[9]->property . $doppelpunkt . $prods->properties->prop[9]->value .$doppelpunkt .'10' .$kommaleerz        
                          .$prods->properties->prop[10]->property . $doppelpunkt . $prods->properties->prop[10]->value .$doppelpunkt .'11' .$kommaleerz        
                          .$prods->properties->prop[11]->property . $doppelpunkt . $prods->properties->prop[11]->value .$doppelpunkt .'12' .$kommaleerz         
                          .$prods->properties->prop[12]->property . $doppelpunkt . $prods->properties->prop[12]->value .$doppelpunkt .'13' .$kommaleerz                
                          .$prods->properties->prop[13]->property . $doppelpunkt . $prods->properties->prop[13]->value .$doppelpunkt .'14' .$kommaleerz
                          .$prods->properties->prop[14]->property . $doppelpunkt . $prods->properties->prop[14]->value .$doppelpunkt .'15' .$kommaleerz        
                          .$prods->properties->prop[15]->property . $doppelpunkt . $prods->properties->prop[15]->value .$doppelpunkt .'16' .$kommaleerz        
                          .$prods->properties->prop[16]->property . $doppelpunkt . $prods->properties->prop[16]->value .$doppelpunkt .'17' .$kommaleerz         
                          .$prods->properties->prop[17]->property . $doppelpunkt . $prods->properties->prop[17]->value .$doppelpunkt .'18' .$kommaleerz 
                          .$prods->properties->prop[18]->property . $doppelpunkt . $prods->properties->prop[18]->value .$doppelpunkt .'19' .$kommaleerz         
                          .$prods->properties->prop[19]->property . $doppelpunkt . $prods->properties->prop[19]->value .$doppelpunkt .'20' .$kommaleerz
                          .'Material' . $doppelpunkt . $prods->material->title .$doppelpunkt .'21' ;} ?> </td>                
            <td><?php echo $bildaddy . $prods->pics->pic[0] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[1] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[2] . $kommaleerz                
                                   .$bildaddy . $prods->pics->pic[3] . $kommaleerz 
                                   .$bildaddy . $prods->pics->pic[4] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[5] . $kommaleerz                
                                   .$bildaddy . $prods->pics->pic[6] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[7] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[8] . $kommaleerz                
                                   .$bildaddy . $prods->pics->pic[9] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[10] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[11] . $kommaleerz                
                                   .$bildaddy . $prods->pics->pic[12] . $kommaleerz 
                                   .$bildaddy . $prods->pics->pic[13] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[14] . $kommaleerz                
                                   .$bildaddy . $prods->pics->pic[15] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[16] . $kommaleerz
                                   .$bildaddy . $prods->pics->pic[17] . $kommaleerz                
                                   .$bildaddy . $prods->pics->pic[18] . $kommaleerz               

                    ; ?></td>          
          </tr>
      <?php endforeach; ?>

       </tbody>
      </table>
    </body>
    </html> 

1 个答案:

答案 0 :(得分:2)

SimpleXML为属性实现不同的接口。如果您像字符串一样使用它,它将返回第一个匹配元素的内容。如果您像列表一样使用它,则可以迭代所有匹配的元素。

$products = new SimpleXmlElement($xml);

foreach ($products->product as $product) {
  foreach ($product->properties->prop as $property) {
    var_dump((string)$property->property);
  }
}

输出:

string(5) "Farbe"
string(13) "Geeignet für"
string(10) "Verpackung"
string(12) "Gesamturteil"
string(8) "Garantie"
string(16) "Art der Garantie"

我建议您为要阅读的每个propid定义一个默认值数组。 propid应该比标题更稳定,而不是语言特定的。在内部循环中,您验证该数组中是否存在当前的id,并分配valuevalueid

foreach ($products->product as $product) {
  // default values for each product property by `prodid`
  $row = [
    '25' => '',
    '41' => ''
  ];
  foreach ($product->properties->prop as $property) {
    $id = (string)$property->propid;
    if (array_key_exists($id, $row)) {
      $row[$id] = (string)$property->value;
    }
  }
  var_dump($row);
}

输出:

array(2) {
  [25]=>
  string(5) "Weiß"
  [41]=>
  string(6) "Unisex"
}

这样,结果将始终具有相同的元素数量和顺序。

获取特定元素(如果存在)

要获取特定属性,请使用Xpath。例如,使用propid 25:

获取属性
foreach ($products->product as $product) {
  $colors = $product->xpath('properties/prop[propid = 25]');
  if (count($colors) > 0) {
    var_dump((string)$colors[0]->property);
  }
}

SimpleXmlElement::xpath()将始终返回SimpleXmlElement个对象的数组。验证元素计数以检查Xpath表达式是否找到节点。

DOM甚至允许您直接获取标量值。如果没有节点匹配,它将返回一个空值。 DOMXpath::evaluate()的结果取决于表达式。位置路径返回DOMNodeList,可foreach遍历$document = new DOMDocument(); $document->loadXml($xml); $xpath = new DOMXpath($document); foreach ($xpath->evaluate('/products/product') as $product) { $hasColor = $xpath->evaluate('count(properties/prop[propid = 25]) > 0', $product); if ($hasColor) { var_dump('Color:', $xpath->evaluate('string(properties/prop[propid = 25]/valueid)', $product)); } } 。导致标量的表达式将返回标量值。

string(6) "Color:"
string(3) "208"

输出:

public List<Employee> GetEmployeesBasesOnBirthDate(int customerId, string birthdate)
    {
        return db.Employees.Where(x => x.CustomerId == customerId && x.BirthDate.Contains(birthdate)).ToList();
    }