【问题标题】:Search XML file with PHP?用 PHP 搜索 XML 文件?
【发布时间】:2016-10-10 21:39:36
【问题描述】:

我有这个 XML 文件 getItems.php:

<items>
  <item name="Designer: X091" price="300">
    <det set="10">
      <data>
        <![CDATA[
        [{"c": 10092, "l": "", "hasItem": false}]
        ]]>
      </data>
    </det>
  </item>
  <item name="Designer: X091" price="10">
    <det set="2">
      <data>
        <![CDATA[
        [{"c": 19920, "l": "", "hasItem": false}]
        ]]>
      </data>
    </det>
  </item>
</items>

我想做的是提取商品的名称和价格,以及det的设置编号,以及data里面的内容到变量中,我想使用foreach所以我可以得到每个项目,如果项目的名称是“设计师:X091”

我正在尝试这个answer,但我对xpath 有点困惑,希望得到一些帮助。谢谢:)

【问题讨论】:

    标签: php


    【解决方案1】:

    这里有一个使用 SimpleXML 搜索特定元素并显示其信息的工作示例。

    我使用while 循环而不是foreach 来在您找到所需元素时停止搜索。

    <?php
    
    $string = '
    <items>
      <item name="Designer: X091" price="300">
        <det set="10">
          <data>
            <![CDATA[
            [{"c": 10092, "l": "", "hasItem": false}]
            ]]>
          </data>
        </det>
      </item>
      <item name="Designer: X091" price="10">
        <det set="2">
          <data>
            <![CDATA[
            [{"c": 19920, "l": "", "hasItem": false}]
            ]]>
          </data>
        </det>
      </item>
    </items>';
    
    $obj = new SimpleXMLElement($string);
    
    $searchedName = 'Designer: X091';
    $numberOfItems = count($obj->item);
    $i = 0;
    
    // While you don't find it and there're elements left, look for the next
    
    while($obj->item[$i]['name'] != $searchedName && $i < $numberOfItems){
      $i++;
    }
    
    // If the counter is NOT less than number of items, we didn't find it
    
    if($i == $numberOfItems){
      echo 'Item not found';
    }
    
    // Else, we know the position of the item in the object
    
    else{
      $price = $obj->item[$i]['price'];
      $detSet = $obj->item[$i]->det['set'];
      $data = $obj->item[$i]->det->data;
    }
    
    echo "Name: $searchedName<br>";
    echo "Price: $price<br>";
    echo "Det set: $detSet<br>";
    echo "Data: $data<br>";
    

    输出是:

    名称:设计师:X091 价格:300 检测组:10 数据:[{“c”:10092,“l”:“”,“hasItem”:false}]

    【讨论】:

      【解决方案2】:

      把你的 XML 放到 $xmlString 变量中,然后:

      // create a new instance of SimpleXMLElement
      $xml = new SimpleXMLElement($xmlString);
      $results = [];
      
      // check how many elements are in your xml
      if ($xml->count() > 0) {
      
          // if more than 0, then create loop
          foreach ($xml->children() as $xmlChild) {
      
              // assign attributes to $attr variable
              $attr = $xmlChild->attributes();
      
              // check if your attrs are defined
              if (isset($attr['name']) && isset($attr['price'])) {
      
                  // attach values to $results array
                  $results[] = [
                      'name' => (string)$attr['name'],
                      'price' => (int)$attr['price']
                  ];
              }
          }
      }
      

      那么变量$results 应该是这样的:

      Array
      (
          [0] => Array
              (
                  [name] => Designer: X091
                  [price] => 300
              )
      
          [1] => Array
              (
                  [name] => Designer: X091
                  [price] => 10
              )
      
      )
      

      【讨论】:

      • 谢谢! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      相关资源
      最近更新 更多