【问题标题】:Loop trough XML with PHP使用 PHP 循环遍历 XML
【发布时间】:2021-11-12 17:35:45
【问题描述】:

我正在尝试循环通过从 API 调用获得的 XML 输出。 XML 的结构如下所示:

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
 <products>
  <product>
   <id>97</id>
   <id_manufacturer>0</id_manufacturer>
   <id_supplier>0</id_supplier>
   <width>0.000000</width>
   <height>0.000000</height>
   <depth>0.000000</depth>
   <weight>1.127272</weight>
   <quantity_discount>0</quantity_discount>
   <on_sale>0</on_sale>
   <online_only>0</online_only>
   <minimal_quantity>1</minimal_quantity>
   <price>37.000000</price>
  </product>
  <product>...</product>
  <product>...</product>
  <product>...</product>
  <product>...</product>
 </products>
</prestashop>

我想循环浏览所有产品并获取例如价格。 我已经尝试过不同的方法,例如:

 $opt = array(
       'resource' => 'products',
       'display'  => 'full',
       'filter[id]' => $_GET['id'],
       'postXml' => 'asXML'
    );

    $xml = $webService->get($opt); // getting the xml data as shown above
    

foreach ($xml->products->product as $p) {
    echo $p->attributes()->price;
}

但结果不会显示任何内容... 如果您能帮助我并可能有解决方案,我会很高兴。

【问题讨论】:

    标签: php xml loops parsing


    【解决方案1】:

    price 不是&lt;product&gt; 的属性,它是一个子节点。因此,例如,如果您想提取价格(即price 元素的字符串值),您可以执行以下操作:

    $dom = new DOMDocument();
    $dom->loadXML($xml);
    $xpath = new DOMXPath($dom);
    
    $products = $xpath->query("//product");
    foreach ($products as $prod)
    {   
       foreach ($xpath->query('.//price',$prod) as $price) {
            echo ($price->textContent)."\n";
     
    }
    };
    

    【讨论】:

    • 感谢您的回答,不幸的是它不起作用。我没有从中得到任何输出..
    • @LiamBonactarian 它适用于您问题中的示例 xml;您的实际 xml 可能与示例有很大不同。
    【解决方案2】:

    上一个答案是正确的,但实际上并没有显示如何循环遍历 XML 对象。

    <?php
    $myXMLData = '<prestashop>
        <products>
            <product>
                <id>97</id>
                <id_manufacturer>0</id_manufacturer>
                <id_supplier>0</id_supplier>
                <width>0.000000</width>
                <height>0.000000</height>
                <depth>0.000000</depth>
                <weight>1.127272</weight>
                <quantity_discount>0</quantity_discount>
                <on_sale>0</on_sale>
                <online_only>0</online_only>
                <minimal_quantity>1</minimal_quantity>
                <price>37.000000</price>
            </product>
            <product>
                <id>98</id>
                <id_manufacturer>0</id_manufacturer>
                <id_supplier>0</id_supplier>
                <width>0.000000</width>
                <height>0.000000</height>
                <depth>0.000000</depth>
                <weight>1.127272</weight>
                <quantity_discount>0</quantity_discount>
                <on_sale>0</on_sale>
                <online_only>0</online_only>
                <minimal_quantity>1</minimal_quantity>
                <price>37.000000</price>
            </product>
        </products>
    </prestashop>';
    
    $xml = simplexml_load_string($myXMLData) or die("Error: Cannot create object");
    foreach ($xml->products->product as $product){
        print_r($product->id);
        /*You can access all of your product object properties here.*/
    }
    ?>
    

    【讨论】:

    • 您好,谢谢您的回答。不幸的是,它不起作用。我不明白 print_r($product->id) 的使用
    • 如果您在命令行环境中运行此代码,print_r 是一个 PHP 函数 php.net/manual/en/function.print-r.php,它将变量输出到屏幕。你也可以使用 var_dump($product);输出整个产品变量php.net/manual/en/function.var-dump
    • 好的。不幸的是,它没有给我任何输出。
    • 您可以尝试创建一个新的 PHP 文件。我测试了它,没有问题。他的输出如下。 ``` SimpleXMLElement 对象 ( [0] => 97 ) SimpleXMLElement 对象 ( [0] => 98 ) ```
    猜你喜欢
    • 2011-10-13
    • 2019-07-07
    • 2013-11-30
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多