[英]Notice: Undefined index of a xml
我有一个小程序,可以将两个xml文件输入到Mysql中。 我有一个PHP代码的麻烦
我不断收到此错误:
Notice: Undefined index: e_cod in on line 327
Notice: Undefined index: nombre in on line 328
Notice: Undefined index: titulo in on line 449
Notice: Undefined index: codbar in on line 450
Notice: Undefined index: precio in on line 451
Notice: Undefined index: editorial in on line 452
Notice: Undefined index: codbar in on line 453
Notice: Undefined index: autor in on line 454
现在这是文件的一些关键部分
313至338行
public function getmanufacturers()
{
$feedurl='http://mysite/manufacturer.xml';
$xml=simplexml_load_file($feedurl);
$products =get_object_vars($xml);
$i=0;
$manufacturer_array =array();
foreach($products as $product)
{
foreach($product as $productitem)
{
$i++;
$feedproduct = (array)($productitem);
$e_cod = $feedproduct['e_cod']; //3
$name = $feedproduct['nombre']; //1
$manufacturer_array['$e_cod'] = $name;
}
}
return $manufacturer_array;
}
和我的XML来自制造商。
<?xml version="1.0" standalone="yes"?>
<RECORDS>
<RECORD>
<id>1</id>
<nombre>AMERICAN BOOK STORE</nombre>
<proveedor_cod>0410</proveedor_cod>
<e_cod>0001</e_cod>
<nombrec>ABS</nombrec>
<cambio>2012/3/14</cambio>
</RECORD>
</RECORDS>
再次从415行到522的PHP文件
$manufacturer_array= $this->getmanufacturers();
//inactive features
$datasource ='TEL';
$feedurl='http://mysite/libro.xml';
$xml=simplexml_load_file($feedurl);
$products =get_object_vars($xml);
$i=0;
$raw_products_arr2 =array();
//$Submitoffset = '894757001973';
// $Submitoffset = 'FL5018-2';
//$Submitoffset = '3000000263';
$gotit =0;
foreach($products as $product)
{
foreach($product as $productitem)
{
$i++;
$feedproduct = (array)($productitem);
//print_r($feedproduct);
//die();
Configuration::updateValue('PRODIMPORTER_STAGE',1);
if($i<$rowid )
{
continue;
}
try
{
$pname = $feedproduct['titulo'];
$reference = $feedproduct['codbar'];
$price = $feedproduct['precio'];
$manufacturer_ref = $feedproduct['editorial'];
$ean13 = $feedproduct['codbar'];
$supplier_reference = $feedproduct['autor'];
if(!isset($feedproduct['id']) || $feedproduct['id']=='')
{
$this->html .= "<br/>Product id not found :".$pname."<br/>";
continue;
}
$pid = $feedproduct['id'];
$quantity =999;
$category_list =array();
$parentcatarray =array();
$wholesale_price =$price;
if($Submitlimit!="0" && $Submitlimit!="" )
{
if((int)$total >= $Submitlimit)
{
break;
}
}
$rowid =$rowid + 1;
$this->rowid = $rowid;
Configuration::updateValue('vcxmlprimporter_counter',$rowid);
$total = (int)$rowid-(int)$startrow;
if(trim($reference)=="" )
{
$this->html .= "<br/>Product reference not found :".$pname."<br/>";
continue;
}
Configuration::updateValue('PRODIMPORTER_LASTPROD',$reference);
if(trim($pname)=="" )
{
$this->html .= "<br/>Product name not found :".$pname."<br/>";
continue;
}
if(!isset($price))
{
$this->html .= "<br/>Price not found :".$pname."<br/>";
continue;
}
if($Submitoffset!="0" && $Submitoffset!="" )
{
if(trim($reference) == $Submitoffset && $gotit ==0)
{
$gotit = 1;
}
else
{
continue;
}
}
我的一些xml文件
<?xml version="1.0" standalone="yes"?>
<RECORDS>
<RECORD>
<id>3</id>
<titulo>INFORMATICA 1 -COMPETENCIAS+APRENDIZAJE+VIDA</titulo>
<autor>ROMERO</autor>
<editorial>0102</editorial>
<tema>0013</tema>
<codbar>3</codbar>
<isbn></isbn>
<precio>225,0000</precio>
</RECORD>
现在,我真的认为由于某种未知原因,assoc数组无法获取我的xml文件的字段。 或我不真正知道的东西
编辑:这与其他问题不同,因为使用xml可以肯定我知道它存在。 (检查xml和PHP)
您没有正确获取PHP
变量中的XML元素值,这就是为什么出现Undefined index
错误的原因。
下面的代码将帮助您从XML
文件中正确获取XML值。
<?php
$xml=simplexml_load_file("http://mysite/manufacturer.xml") or die("Error:
Cannot create object");
$count = count($xml->RECORD);
for($i=0;$i<$count;$i++){
$id = $xml->RECORD[$i]->id;
$titulo = $xml->RECORD[$i]->titulo;
$autor = $xml->RECORD[$i]->autor;
$editorial = $xml->RECORD[$i]->editorial;
$tema = $xml->RECORD[$i]->tema;
$codbar = $xml->RECORD[$i]->codbar;
$isbn = $xml->RECORD[$i]->isbn;
$precio = $xml->RECORD[$i]->precio;
// Execute MySQL Query like Insert related to data received
}
?>
如果要在PHP数组变量中添加所有XML元素以供以后使用,请执行以下操作。
<?php
$xml=simplexml_load_file("http://mysite/manufacturer.xml") or die("Error:
Cannot create object");
$count = count($xml->RECORD);
for($i=0;$i<$count;$i++){
$id[] = $xml->RECORD[$i]->id;
$titulo[] = $xml->RECORD[$i]->titulo;
$autor[] = $xml->RECORD[$i]->autor;
$editorial[] = $xml->RECORD[$i]->editorial;
$tema[] = $xml->RECORD[$i]->tema;
$codbar[] = $xml->RECORD[$i]->codbar;
$isbn[] = $xml->RECORD[$i]->isbn;
$precio[] = $xml->RECORD[$i]->precio;
}
?>
希望能帮助到你!
访问SimpleXML元素时-访问元素的属性时使用[]
。 因此,您应该使用->
...
public function getmanufacturers()
{
$feedurl='http://mysite/manufacturer.xml';
$xml=simplexml_load_file($feedurl);
$manufacturer_array =array();
foreach($xml->RECORDS as $product)
{
foreach($product->RECORD as $productitem)
{
$e_cod = $productitem->e_cod; //3
$name = $productitem->nombre; //1
$manufacturer_array['$e_cod'] = $name;
}
}
return $manufacturer_array;
}
我还删除了一些并不是真正有用的编码。 将foreach
更改为使用$xml->RECORDS
表示要遍历XML的每个<RECORDS>
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.