【问题标题】:Simplexml is giving me the wrong resultsSimplexml 给了我错误的结果
【发布时间】:2011-01-07 15:22:04
【问题描述】:

下面有一个简单的xml:

<?xml version="1.0" encoding="utf-8"?>
<catalogue>
  <category name="textbook" id="100" parent="books">
    <product id="20000">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
    </product>
    <product id="20001">
      <author>Gambardellas, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
    </product>
  </category>
  <category name="fiction" id="101" parent="books">
    <product id="2001">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <type>Fiction</type>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, an evil sorceress,                  and her own childhood to become queen
      of the world.</description>
    </product>
  </category>
</catalogue>

我正在使用 php simplexml 库对其进行如下解析:(注意有两个类别节点。第一个类别包含两个“产品”子级。我的目标是获取一个包含第一个“类别”的两个子级的数组

$xml = simplexml_load_file($xml_file) or die ("unable to load XML File!".$xml_file);

//for each product, print out info
$cat = array();
foreach($xml->category as $category)
{
    if($category['id'] == 100)
    {
        $cat = $category;       
        break;
    }
}
$prod_arr = $category->product;

这就是问题所在。我期待一个包含两个子产品的数组,但它只返回一个产品。我在做什么错还是这是一个 php 错误?请帮忙!

【问题讨论】:

    标签: php xml dom simplexml


    【解决方案1】:

    您可以使用SimpleXMLElement::xpath() 来获取属于特定类别元素的所有产品元素。例如

     // $catalogue is your $xml
     $products = $catalogue->xpath('category[@id="100"]/product');
     foreach($products as $p) {
       echo $p['id'], ' ', $p->title, "\n";
     }
    

    打印

    20000 XML Developer's Guide
    20001 XML Developer's Guide
    

    【讨论】:

      【解决方案2】:

      首先,您的 XML 文件定义不明确。你可能应该用&lt;categories&gt;标签开始和结束它。

      用以下内容替换最后的分配:

      $prod_array = array();
      foreach ($cat->product as $p) {
          $prod_array[] = $p;
      }
      

      【讨论】:

        【解决方案3】:
        $cat = array();
        foreach ($xml->category as $category)
        {
            $attributes = $category->attributes();
            if(isset($attributes['id']) && $attributes['id'] == 100)
            {
                $cat = $category;
                break;
            }
        }
        

        【讨论】:

        • 在指责您的代码之前,不要忘记验证 XML 文件。这很简单,只要用 firefox 打开你的文件(或者在 Linux 下试试 xmllint 命令),它会告诉你错误在哪里。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-13
        • 2016-05-31
        • 2020-09-15
        相关资源
        最近更新 更多