循环通过SimpleXMLElement

时间:2016-02-22 13:14:47

标签: php xml

我有以下SimpleXMLElement:

object(SimpleXMLElement)#2 (1) {
  ["PRODUCT"]=>
  array(19) {
    [0]=>
    object(SimpleXMLElement)#4 (13) {
      ["PRODUCT_NAME"]=>
      string(51) "S5 16GB SM-G900F LTE יבוא רשמי במלאי !"
      ["CATALOG_NUMBER"]=>
      object(SimpleXMLElement)#23 (0) {
      }
      ["MODEL"]=>
      object(SimpleXMLElement)#24 (0) {
      }
      ["DETAILS"]=>
      object(SimpleXMLElement)#25 (0) {
      }
      ["PRODUCT_URL"]=>
      string(32) "http://lcdeal.co.il/items/285885"
      ["CURRENCY"]=>
      string(3) "ILS"
      ["PRICE"]=>
      string(6) "1890.0"
      ["SHIPMENT_COST"]=>
      string(3) "0.0"
      ["DELIVERY_TIME"]=>
      string(1) "7"
      ["WARRANTY"]=>
      string(45) " 12 יבואן רשמי "סאני" "
      ["PRODUCT_TYPE"]=>
      string(16) "מוצר חדש."
      ["MANUFACTURER"]=>
      string(7) "Samsung"
      ["IMAGE"]=>
      string(101) "https:/konimboimages.s3.amazonaws.com/system/photos/325233/large/892d72fb2cc001055f1120d7e386809b.jpg"
    }
    [1]=>
    object(SimpleXMLElement)#5 (13) {
      ["PRODUCT_NAME"]=>
      string(50) "LG G3 32GB D855 אופציה ליבוא רשמי !"
      ["CATALOG_NUMBER"]=>
      object(SimpleXMLElement)#25 (0) {
      }
      ["MODEL"]=>
      object(SimpleXMLElement)#24 (0) {
      }
      ["DETAILS"]=>
      object(SimpleXMLElement)#23 (0) {
      }
      ["PRODUCT_URL"]=>
      string(32) "http://lcdeal.co.il/items/316277"
      ["CURRENCY"]=>
      string(3) "ILS"
      ["PRICE"]=>
      string(6) "1490.0"
      ["SHIPMENT_COST"]=>
      string(3) "0.0"
      ["DELIVERY_TIME"]=>
      string(1) "7"
      ["WARRANTY"]=>
      string(99) "12 חודשי אחריות יו.פי או 24 חודשים יבוא רשמי בתוספת תשלום"
      ["PRODUCT_TYPE"]=>
      string(16) "מוצר חדש."
      ["MANUFACTURER"]=>
      string(2) "Lg"
      ["IMAGE"]=>
      string(101) "https:/konimboimages.s3.amazonaws.com/system/photos/368234/large/31be1a6e8aaecb3a65bbf3e1e04981a3.jpg"
    }
    [2]=>
    object(SimpleXMLElement)#6 (13) {
      ["PRODUCT_NAME"]=>
      string(39) "S6 SM-G920F 32GB  זמין במלאי !"
      ["CATALOG_NUMBER"]=>
      object(SimpleXMLElement)#23 (0) {
      }
      ["MODEL"]=>
      object(SimpleXMLElement)#24 (0) {
      }
      ["DETAILS"]=>
      object(SimpleXMLElement)#25 (0) {
      }
      ["PRODUCT_URL"]=>
      string(32) "http://lcdeal.co.il/items/510594"
      ["CURRENCY"]=>
      string(3) "ILS"
      ["PRICE"]=>
      string(6) "2490.0"
      ["SHIPMENT_COST"]=>
      string(3) "0.0"
      ["DELIVERY_TIME"]=>
      string(1) "7"
      ["WARRANTY"]=>
      string(54) "12 חודשים יבוא רשמי "סאני""
      ["PRODUCT_TYPE"]=>
      string(16) "מוצר חדש."
      ["MANUFACTURER"]=>
      string(7) "Samsung"
      ["IMAGE"]=>
      string(101) "https:/konimboimages.s3.amazonaws.com/system/photos/659080/large/fcbe9ef792f79cfdbfff9ad2fe995a28.jpg"
    }
  ***AND SO ON***
  }
}

现在,合乎逻辑的做法是遍历PRODUCT键,但当我尝试循环它时,我只得到第一个元素:

object(SimpleXMLElement)#4 (13) {
  ["PRODUCT_NAME"]=>
  string(51) "S5 16GB SM-G900F LTE יבוא רשמי במלאי !"
  ["CATALOG_NUMBER"]=>
  object(SimpleXMLElement)#6 (0) {
  }
  ["MODEL"]=>
  object(SimpleXMLElement)#7 (0) {
  }
  ["DETAILS"]=>
  object(SimpleXMLElement)#8 (0) {
  }
  ["PRODUCT_URL"]=>
  string(32) "http://lcdeal.co.il/items/285885"
  ["CURRENCY"]=>
  string(3) "ILS"
  ["PRICE"]=>
  string(6) "1890.0"
  ["SHIPMENT_COST"]=>
  string(3) "0.0"
  ["DELIVERY_TIME"]=>
  string(1) "7"
  ["WARRANTY"]=>
  string(45) " 12 יבואן רשמי "סאני" "
  ["PRODUCT_TYPE"]=>
  string(16) "מוצר חדש."
  ["MANUFACTURER"]=>
  string(7) "Samsung"
  ["IMAGE"]=>
  string(101) "https:/konimboimages.s3.amazonaws.com/system/photos/325233/large/892d72fb2cc001055f1120d7e386809b.jpg"
}

那么如何遍历这个SimpleXMLElement并获取其所有值(我使用的是PHP)?

2 个答案:

答案 0 :(得分:0)

从我记忆中,这应该可以正常运作:

$xml = simplexml_load_string('...');
foreach ($xml->PRODUCT as $product) {
    // $product should contain one product node and foreach loop should execute for every node
    $productName = (string)$product->PRODUCT_NAME;
}

答案 1 :(得分:0)

使用children()方法解决:

$rss = simpleXML_load_file('LINK');
$products = $rss->PRODUCTS;
foreach ($xml->children() as $product) {
    $productName = (string) $product->PRODUCT_NAME;
}