simplexml 给出致命错误:在第 136 行的非对象上调用成员函数 attributes(),并在注释掉第 156


simplexml gives Fatal error: Call to a member function attributes() on a non-object in line 136 and when commented out line 156

使用我的代码,我正在尝试从xml(曾经是svg)文件中获取路径数据,虽然我能够通过$attributes数组成功打印出数据,但使用echo它仍然在(第136行和第156行)中的非对象上拉出"对成员函数属性()的调用"我根本看不出代码有什么问题,尽管最坏的情况我总是可以使用一个技巧,将所选数据插入 while 循环中的数据库并在迭代时向前迭代到所需的页面......我想知道我的代码到底出了什么问题。它使用 echo 命令回显出正确的信息。它只是仍然在非对象错误时调用成员函数

while($number_of_array_path >=1)
{
//declares attributes based on path number for sodipodi attributes line136
$attributes = $xml_file_open->g->path[$path_incremental_counter]-
>attributes('sodipodi', true); 
//sets each attribute within an array and uses the path incremental counter for 
//that pourpose.
$type_array[$path_incremental_counter] = $attributes['type'];
$cx_array[$path_incremental_counter] = $attributes['cx'];
$cy_array[$path_incremental_counter] = $attributes['cy'];
$rx_array[$path_incremental_counter] = $attributes['rx'];
$ry_array[$path_incremental_counter] = $attributes['ry'];
echo $type_array[$path_incremental_counter];
echo "<br/>";                               
echo $cx_array[$path_incremental_counter];
echo "<br/>";                               
echo $cy_array[$path_incremental_counter];
echo "<br/>";
echo $rx_array[$path_incremental_counter];
echo "<br/>";
echo $ry_array[$path_incremental_counter];
echo "<br/>";                               

//same as above but for non"sodipodi" attributes. line 156
$attributes = $xml_file_open->g->path[$path_incremental_counter]->attributes();
// echo $attributes['id'];  
$style_array[$path_incremental_counter] = $attributes['style'];
$id_array[$path_incremental_counter] = $attributes['id'];
$d_array[$path_incremental_counter] = $attributes['d'];
echo $style_array[$path_incremental_counter];
echo "<br/>";
echo $id_array[$path_incremental_counter];
echo "<br/>";
echo $d_array[$path_incremental_counter];
echo "<br/>";

//increments path incremental counter
$path_incremental_counter = $path_incremental_counter + 1;
//decrements number of array path
$number_of_array_path = $number_of_array_path;                                                              
}
foreach($xml_file_open->g->path[$shape_path_iteration_counter]->attributes() 
as $attribute => $value)

对于非钠波迪元素

foreach($xml_file_open->g->path[$shape_path_iteration_counter]->
attributes('sodipodi', true) as $attribute => $value)

对于索迪波迪元素。