通过用SimpleXML XML对象循环通过用SimpleXML XML对象循环(Loop thro

2019-05-12 06:25发布

我想通过一个XML对象使用的SimpleXML PHP中环 - 对象格式如下: -

我收到以下错误,当我试图让通过名称的元素标签 - 任何人都可以解释我做错了吗?

Call to undefined method SimpleXMLElement::getElementsByTagName() on ...

下面是我从读XML文件:

<?xml version="1.0" encoding="utf-8"?>
<response>
    <products>
        <item>
            <product_id>32417</product_id>
            <manufacturer>Alcatel</manufacturer>
            <model>Sparq 2</model>
            <deeplink>http://www.mysite.com/sc_offer?gid=32417</deeplink>
            <thumbnail_URL>http://www.mysite.com/images/devices/thumbs/Alcatel-Sparq-II.jpg</thumbnail_URL>
            <image_URL>http://www.mysite.com/images/devices/Alcatel-Sparq-II.jpg</image_URL>
            <price_not_working>0.00</price_not_working>
            <price_poor>0.00</price_poor>
            <price_fair>20.00</price_fair>
            <price_good>25.00</price_good>
            <price_perfect>25.00</price_perfect>
            <price_new>25.00</price_new>
            <battery_new>1.00</battery_new>
            <battery_perfect>1.00</battery_perfect>
            <battery_good>1.00</battery_good>
            <battery_fair>1.00</battery_fair>
            <battery_poor>0.00</battery_poor>
            <charger_new>1.00</charger_new>
            <charger_perfect>1.00</charger_perfect>
            <charger_good>1.00</charger_good>
            <charger_fair>1.00</charger_fair>
            <charger_poor>0.00</charger_poor>
            <packaging_new>1.00</packaging_new>
            <packaging_perfect>1.00</packaging_perfect>
            <packaging_good>1.00</packaging_good>
            <packaging_fair>1.00</packaging_fair>
            <packaging_poor>0.00</packaging_poor>
        </item>
        <item>
            <product_id>31303</product_id>
            <manufacturer>Apple</manufacturer>
            <model>iPhone 3G 8gb</model>
            <deeplink>http://www.mysite.com/sc_offer?gid=31303</deeplink>
            <thumbnail_URL>http://www.mysite.com/images/devices/thumbs/iPhone 8 3G.jpg</thumbnail_URL>
            <image_URL>http://www.mysite.com/images/devices/iPhone 8 3G.jpg</image_URL>
            <price_not_working>0.00</price_not_working>
            <price_poor>0.00</price_poor>
            <price_fair>7.00</price_fair>
            <price_good>2.00</price_good>
            <price_perfect>2.00</price_perfect>
            <price_new>2.00</price_new>
            <battery_new>1.00</battery_new>
            <battery_perfect>1.00</battery_perfect>
            <battery_good>1.00</battery_good>
            <battery_fair>1.00</battery_fair>
            <battery_poor>0.00</battery_poor>
            <charger_new>1.00</charger_new>
            <charger_perfect>1.00</charger_perfect>
            <charger_good>1.00</charger_good>
            <charger_fair>1.00</charger_fair>
            <charger_poor>0.00</charger_poor>
            <packaging_new>1.00</packaging_new>
            <packaging_perfect>1.00</packaging_perfect>
            <packaging_good>1.00</packaging_good>
            <packaging_fair>1.00</packaging_fair>
            <packaging_poor>0.00</packaging_poor>
        </item>
    </products>
</response>

我的PHP代码如下:

$devices = $xml->getElementsByTagName( "response" ); // error on this line

我努力使器件阵列,所以我可以在foreach循环使用这些数据。

Answer 1:

SimpleXML的不具有getElementsByTagName()方法(DOM文档一样)。

SimpleXML中,对象(例如, $xml )被视为根元素。 所以,你可以循环通过产品项目,像这样:

$xml = simplexml_load_string($xmlString);
foreach($xml->products->item as $item)
{
    echo (string)$item->product_id;
    echo (string)$item->model;
}

构建设备关联数组的实施例:

$devices = array();

$xml = simplexml_load_string($xmlString);
foreach($xml->products->item as $item)
{
   $device = array();

   foreach($item as $key => $value)
   {
        $device[(string)$key] = (string)$value;
   }

   $devices[] = $device;
}

print_r($devices);

输出:

Array
(
    [0] => Array
        (
            [product_id] => 32417
            [manufacturer] => Alcatel
            [model] => Sparq 2
            [deeplink] => http://www.mysite.com/sc_offer?gid=32417
            [thumbnail_URL] => http://www.mysite.com/images/devices/thumbs/Alcatel-Sparq-II.jpg
            [image_URL] => http://www.mysite.com/images/devices/Alcatel-Sparq-II.jpg
            [price_not_working] => 0.00
            [price_poor] => 0.00
            [price_fair] => 20.00
            [price_good] => 25.00
            [price_perfect] => 25.00
            [price_new] => 25.00
            [battery_new] => 1.00
            [battery_perfect] => 1.00
            [battery_good] => 1.00
            [battery_fair] => 1.00
            [battery_poor] => 0.00
            [charger_new] => 1.00
            [charger_perfect] => 1.00
            [charger_good] => 1.00
            [charger_fair] => 1.00
            [charger_poor] => 0.00
            [packaging_new] => 1.00
            [packaging_perfect] => 1.00
            [packaging_good] => 1.00
            [packaging_fair] => 1.00
            [packaging_poor] => 0.00
        )

    [1] => Array
        (
            [product_id] => 31303
            [manufacturer] => Apple
            [model] => iPhone 3G 8gb
            [deeplink] => http://www.mysite.com/sc_offer?gid=31303
            [thumbnail_URL] => http://www.mysite.com/images/devices/thumbs/iPhone 8 3G.jpg
            [image_URL] => http://www.mysite.com/images/devices/iPhone 8 3G.jpg
            [price_not_working] => 0.00
            [price_poor] => 0.00
            [price_fair] => 7.00
            [price_good] => 2.00
            [price_perfect] => 2.00
            [price_new] => 2.00
            [battery_new] => 1.00
            [battery_perfect] => 1.00
            [battery_good] => 1.00
            [battery_fair] => 1.00
            [battery_poor] => 0.00
            [charger_new] => 1.00
            [charger_perfect] => 1.00
            [charger_good] => 1.00
            [charger_fair] => 1.00
            [charger_poor] => 0.00
            [packaging_new] => 1.00
            [packaging_perfect] => 1.00
            [packaging_good] => 1.00
            [packaging_fair] => 1.00
            [packaging_poor] => 0.00
        )

)


Answer 2:

我不想破坏现有的答案 ,因为它是在一般的方式回答正确的。

为了您的具体要求与你的XML不会有任何属性,你就只是为了寻找元素名称=>节点-值对在这里,有自带与的SimpleXMLElement这里结合想到一个功能: get_object_vars

当你转换一个对象到一个数组,为的SimpleXMLElement变成元素名称为对象属性名称和节点值的属性值,这是很简单的在这里它是有用的:

$xml     = simplexml_load_string($buffer);

$items   = $xml->products->item;

$devices = array_map('get_object_vars', iterator_to_array($items, FALSE));

print_r($devices);

输出是在现有的答案建议。 和在线演示是在这里: https://eval.in/57109 。

你可能能够获得类似的结果与铸造阵列(如果不是完全用SimpleXML一样),但是在这种情况下,我想它映射,我需要一个真正的功能。

还有一个JSON-恩和岛区的代码转换完成树木翻番,其中就派上用场了这里,太:

$xml     = simplexml_load_string($buffer);

$items   = $xml->products;

$devices = json_decode(json_encode($items), TRUE)['item'];

然后,再次输出是完全按照现有的答案。 和在线演示又是在这里: https://eval.in/57117

希望这是有益的,加宽视了一下。



文章来源: Loop through an XML object with SimpleXML