简体   繁体   English

在PHP中解析xml时循环?

[英]For loop while parsing xml in php?

I am trying to create a for loop that will parse out different elements of xml doc using php. 我正在尝试创建一个for循环,它将使用php解析xml doc的不同元素。 It parses when I put the number of the entry in the line: 当我在行中输入条目号时,它会解析:

$s = $xml->entry[0]->children($namespaces['s']); 

However I tried to replace the number with "i" to count through each of the entries in the xml doc and I get an error (Fatal error: Call to a member function children() on a non-object in /home/content/c/a/s/cashme/html/buylooper/xml.php on line 10). 但是我试图用“i”替换数字来计算xml doc中的每个条目,我得到一个错误(致命错误:在/ home / content /中的非对象上调用成员函数children()第10行的c / a / s / cashme / html / buylooper / xml.php)。 How do I resolve this? 我该如何解决这个问题?

<?php
$url = 'xml-file.xml'; 
$xml = simplexml_load_file($url); 
$namespaces = $xml->entry->getNameSpaces(true); 

for ($i = 0; $i <= 10; $i += 1){

$s = $xml->entry[i]->children($namespaces['s']); 
$title = $s->product->title; 

//print 
echo $retailer;

}
?>

You need to use $i, instead of i. 你需要使用$ i而不是i。

As a side note: 作为旁注:

This bug should become very obvious on a development machine with: 在开发机器上,这个bug应该变得非常明显:

you would get a warning like Use of undefined constant i - assumed 'i' 你会得到一个警告,比如Use of undefined constant i - assumed 'i'

(unless you have defined i as a constant somewhere but that's an odd constant name) (除非你已经将i定义为某个常量,但这是一个奇怪的常量名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM