仅获取XML中父标记的几个标记并将它们放入数组中

时间:2020-10-06 09:42:21

标签: xml wordpress

我有这种XML格式:

<item>
 <sku>10064</sku>
 <price>22.0000</price>
 <pricelist>#TL#</pricelist>
 <new_arrival/>
 <stock>3</stock>
 <photo>10602</photo>
 <packing/>
 <model>RS-232 SINGLE</model>
 <cat>
  <![CDATA[ ΣΥΝΑΓΕΡΜΟΙ | ΑΣΥΡΜΑΤΟΙ ΣΥΝΑΓΕΡΜΟΙ ]]>
 </cat>
 <title>
  <![CDATA[ ΘΥΡΑ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ VISONIC ]]>
 </title>
 <description>
  <![CDATA[ <div class="h3-d-1"></div> ]]>
 </description>
</item>

我要在这里实现的目标是仅获得父标记项目的4个名称标记( sku,价格,库存,型号),然后将它们放在数组,然后使用file_put_contents将数组的数据放入文件中。

我正在运行的代码是:

<?php

/*
Plugin Name: teleXML1
Plugin URI: https://gofunctest.info
Description: Used to get product lists based on database
Version: 1.0
Author: go_func
Author URI: 
License: GPL2
*/

    
require_once('public_html/wp-load.php');
require_once('public_html/wp-admin/includes/file.php');
require_once('public_html/wp-admin/includes/media.php');


$url = "http://www.tele.gr/xml/items";
$xml = simplexml_load_file($url);

$each_item              = array();
$result_array_form      = array();
$arr = (array) $xml->item;

if (!(empty($arr)))
    {
        foreach($xml->item as $item) {
            $each_item = (array) $item;
            array_push($result_array_form, $each_item );
        }
    }
    
$result_array_form_1    = array();
file_put_contents( 'public_html/wp-content/plugins/xmls/tele_1.bin' , serialize($result_array_form_1));
    

foreach($result_array_form as $item) {
      print_r ("<pre>");
      print_r ($item);
      print_r ("</pre>");
    }

?>

我收到的所有项目的输出如下:

<pre>Array
(
    [sku] => 10064
    [price] => 22.0000
    [pricelist] => #TL#
    [new_arrival] => SimpleXMLElement Object
        (
        )

    [stock] => 3
    [photo] => 10602
    [packing] => SimpleXMLElement Object
        (
        )

    [model] => RS-232 SINGLE
    [cat] => SimpleXMLElement Object
        (
        )

    [title] => SimpleXMLElement Object
        (
        )

    [description] => SimpleXMLElement Object
        (
        )

)
</pre>

最后,运行代码后,我试图在其中放置内容的文件 tele_1.bin 似乎是空的,并且未对数据进行序列化。它仅包含以下内容:a:0:{}

所以,我想这个问题是由输出 SimpleXMLElement Object 的标签引起的,这就是为什么我不想使用它们。毕竟,我在项目中不需要它们的值。

我希望这里一切都有意义。我从事一个WordPress项目。我将对此表示欢迎和感谢。

提前谢谢乔治。

0 个答案:

没有答案
相关问题