Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将XML转换为CSV,在某些iTen中使用不同的字段_Php_Xml_Csv_Data Conversion_Google Shopping - Fatal编程技术网

使用PHP将XML转换为CSV,在某些iTen中使用不同的字段

使用PHP将XML转换为CSV,在某些iTen中使用不同的字段,php,xml,csv,data-conversion,google-shopping,Php,Xml,Csv,Data Conversion,Google Shopping,我想将XML转换为CSV。没关系,但在某些ITEN中,我比其他ITEN有更多的字段或更少的字段 我的提要的一个示例是: 编辑:XML的顶部是: <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> <channel> <item> 字段较少的项目: <item> <title> <![CDATA[ Resident Evil Revelations 2: Ra

我想将XML转换为CSV。没关系,但在某些ITEN中,我比其他ITEN有更多的字段或更少的字段

我的提要的一个示例是:

编辑:XML的顶部是:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<item>

字段较少的项目:

<item>
<title>
<![CDATA[
Resident Evil Revelations 2: Raid Mode: Throwback Map Pack
]]>
</title>
<link>
https://www.nuuvem.com/item/resident-evil-revelations-2-raid-mode-throwback-map-pack
</link>
<description>
<![CDATA[
Novas missões do modo raide! 4 mapas nostálgicos de locais icônicos, como o Queen Zenobia, do Resident Evil Revelations. 3 níveis de dificuldade oferecem um total de 12 novas missões.
]]>
</description>
<g:availability>out of stock</g:availability>
<g:price currency="BRL">9.99</g:price>
<g:image_link>
http://dskhvldhwok3h.cloudfront.net/image/upload/t_boxshot_big/v1/products/5584854f69702d7235000025/boxshots/j6qaxbrhowfkijd5zdg8.jpg
</g:image_link>
<g:product_type>
<![CDATA[ Action ]]>
</g:product_type>
<g:google_product_category>Software > Video Game Software > Computer Games</g:google_product_category>
<g:condition>new</g:condition>
<g:identifier_exists>FALSE</g:identifier_exists>
<g:id>11985</g:id>
</item>

https://www.nuuvem.com/item/resident-evil-revelations-2-raid-mode-throwback-map-pack
缺货
9.99
http://dskhvldhwok3h.cloudfront.net/image/upload/t_boxshot_big/v1/products/5584854f69702d7235000025/boxshots/j6qaxbrhowfkijd5zdg8.jpg
软件>视频游戏软件>电脑游戏
新的
错误的
11985
具有最常用字段的项目:

<item>
<title>
<![CDATA[ Tom Clancys Rainbow Six - SIEGE: Gemstone Bundle ]]>
</title>
<link>
https://www.nuuvem.com/bundle/tom-clancy-s-rainbow-six-siege-gemstone-bundle
</link>
<description>
<![CDATA[ ]]>
</description>
<g:availability>in stock</g:availability>
<g:price currency="BRL">38.99</g:price>
<g:image_link>
http://dskhvldhwok3h.cloudfront.net/image/upload/t_boxshot_big/v1/products/573ded74f372803be9006b35/boxshots/l8ypqwhq48jzbxogypeh.jpg
</g:image_link>
<g:product_type>
<![CDATA[ Bundle ]]>
</g:product_type>
<g:google_product_category>Software > Video Game Software > Computer Games</g:google_product_category>
<g:condition>new</g:condition>
<g:identifier_exists>FALSE</g:identifier_exists>
<g:id>12705</g:id>
</item>

https://www.nuuvem.com/bundle/tom-clancy-s-rainbow-six-siege-gemstone-bundle
有现货的
38.99
http://dskhvldhwok3h.cloudfront.net/image/upload/t_boxshot_big/v1/products/573ded74f372803be9006b35/boxshots/l8ypqwhq48jzbxogypeh.jpg
软件>视频游戏软件>电脑游戏
新的
错误的
12705
包含更多字段的项目:

<item>
<title>
<![CDATA[ Far Cry 4 - Gold Edition ]]>
</title>
<link>https://www.nuuvem.com/item/far-cry-4-gold-edition</link>
<description>
<![CDATA[
You are a gun for hire, trapped in a war-torn African state, stricken with malaria and forced to make deals with corrupt warlords on both sides of the conflict in order to make this country your home. You must identify and exploit your enemies' weaknesses, neutralizing their superior numbers and firepower.
]]>
</description>
<g:availability>in stock</g:availability>
<g:price currency="BRL">129.99</g:price>
<g:sale_price currency="BRL">64.99</g:sale_price>
<g:sale_price_effective_date>
2017-01-26T02:00:00+00:00/2017-01-31T01:59:00+00:00
</g:sale_price_effective_date>
<g:image_link>
http://dskhvldhwok3h.cloudfront.net/image/upload/t_boxshot_big/v1/products/557dbc5369702d0a9c57e600/boxshots/ld6c69odlluoerzmwyga.jpg
</g:image_link>
<g:product_type>
<![CDATA[ Action ]]>
</g:product_type>
<g:google_product_category>Software > Video Game Software > Computer Games</g:google_product_category>
<g:condition>new</g:condition>
<g:identifier_exists>FALSE</g:identifier_exists>
<g:id>2246</g:id>
</item>

https://www.nuuvem.com/item/far-cry-4-gold-edition
$filexml='file.xml';
如果(文件_存在($filexml)){
$xml=simplexml\u加载文件($filexml);
$i=1;//位置计数器
$values=[];//PHP数组
//写入列标题
$columns=array('title','link','description','availability','price','image_link','product_type','google_product_category','condition','identifier_exists','id');
$fs=fopen('nuuvem-merchant.csv','w');
fputcsv($fs,$columns);
现金流量表(fs);
//遍历每个节点
$node=$xml->xpath('//item');
foreach($n节点){
//遍历节点的每个子节点
$child=$xml->xpath('//item['.$i.]/*');
foreach($child作为$value){
$values[]=$value;
}
//写入CSV文件(附加到列标题)
$fs=fopen('nuuvem-merchant.csv','a');
fputcsv($fs,$value);
现金流量表(fs);
$values=[];//清除下一行的数组
$i++;//移动到下一个位置(即节点位置)
}
}
使用这个脚本,我总是得到每个项目字段的顺序。因此,数据多次与列的标题不匹配,并且一些具有更多字段的项因此没有标题

对不起,我不是PHP开发人员,解决这个问题对我来说很难。我试图在这里搜索,但没有发现像我这样的问题


谢谢

只需对CSV标题使用的
数组进行迭代,在该数组中,您将列名传递到
XPath
表达式中,而不是所有项的子项都使用
/*
。使用
[0]
索引选择返回的XPath数组的第一项,并使用
trim()
删除空白

此外,您还需要注册名称空间前缀
g
,以访问这些元素(因此名称空间始终包含在发布的XML片段中非常重要):

$filexml='GoogleProductFeed.xml';
$xml=simplexml\u加载文件($filexml);
$xml->registerXPathNamespace('g','http://base.google.com/ns/1.0');
如果(file_存在($filexml)){
$xml=simplexml\u加载文件($filexml);
$i=1;//位置计数器
$values=[];//PHP数组
//写入列标题
$columns=array('title','link','description','g:availability','g:price','g:image\u link','g:product\u type',',
“g:google_产品_类别”、“g:condition”、“g:identifier_存在”、“g:id”);
$fs=fopen('GoogleProductFeed.csv','w');
fputcsv($fs,$columns);
现金流量表(fs);
//遍历每个节点
$node=$xml->xpath('//item');
foreach($n节点){
//遍历节点的每个子节点
foreach($col列){
if(count($xml->xpath('//item['.$i.]/'.$col))>0){
$values[]=trim($xml->xpath('//item['.$i.]/'.$col)[0]);
}否则{
$values[]='';
}    
}    
//写入CSV文件(附加到列标题)
$fs=fopen('GoogleProductFeed.csv','a');
fputcsv($fs,$value);
现金流量表(fs);
$values=[];//清除下一行的数组
$i++;//移动到下一个位置(即节点位置)
}
}

请注意,对于那些适用的列,列标题将具有
g:
前缀。可能使用两个几乎相同的数组,一个用于标题,另一个用于XPath调用。

只需迭代CSV标题使用的
数组,在该数组中将列名传递到
XPath
表达式中,而不是所有项的子项都使用
/*
。使用
[0]
索引选择返回的XPath数组的第一项,并使用
trim()
删除空白

此外,您还需要注册名称空间前缀
g
,以访问这些元素(因此名称空间始终包含在发布的XML片段中非常重要):

$filexml='GoogleProductFeed.xml';
$xml=simplexml\u加载文件($filexml);
$xml->registerXPathNamespace('g','http://base.google.com/ns/1.0');
如果(file_存在($filexml)){
$xml=simplexml\u加载文件($filexml);
$i=1;//位置计数器
$values=[];//PHP数组
//写入列标题
$columns=array('title','link','description','g:availability','g:price','g:image\u link','g:product\u type',',
“g:google_产品_类别”、“g:condition”、“g:identifier_存在”、“g:id”);
$fs=fopen('GoogleProductFeed.csv','w');
fputcsv($fs,$columns);
现金流量表(fs);
//遍历每个节点
$node=$xml->xpath('//item');
foreach($n节点){
//遍历节点的每个子节点
foreach($col列){
if(count($xml->xpath('//item['.$i.]/'.$col))>0){
$values[]=trim($xml->xpath($xml)/
$filexml='file.xml';
if (file_exists($filexml))  {

   $xml = simplexml_load_file($filexml);
   $i = 1;           // Position counter
   $values = [];     // PHP array

   // Writing column headers
   $columns = array('title', 'link', 'description', 'availability', 'price', 'image_link', 'product_type', 'google_product_category', 'condition', 'identifier_exists', 'id');

   $fs = fopen('nuuvem-merchant.csv', 'w');
   fputcsv($fs, $columns);      
   fclose($fs);

   // Iterate through each <item> node
   $node = $xml->xpath('//item');

   foreach ($node as $n) {           

       // Iterate through each child of <item> node
       $child = $xml->xpath('//item['.$i.']/*');      

       foreach ($child as $value) {
          $values[] = $value;         
       }

       // Write to CSV files (appending to column headers)
       $fs = fopen('nuuvem-merchant.csv', 'a');
       fputcsv($fs, $values);      
       fclose($fs);  

       $values = [];    // Clean out array for next <item> (i.e., row)
       $i++;            // Move to next <item> (i.e., node position)
   }
}
$filexml = 'GoogleProductFeed.xml';
$xml = simplexml_load_file($filexml);
$xml->registerXPathNamespace('g', 'http://base.google.com/ns/1.0');

if (file_exists($filexml))  {    
   $xml = simplexml_load_file($filexml);
   $i = 1;           // Position counter
   $values = [];     // PHP array

   // Writing column headers
   $columns = array('title', 'link', 'description', 'g:availability', 'g:price', 'g:image_link', 'g:product_type',
                    'g:google_product_category', 'g:condition', 'g:identifier_exists', 'g:id');

   $fs = fopen('GoogleProductFeed.csv', 'w');
   fputcsv($fs, $columns);      
   fclose($fs);

   // Iterate through each <item> node
   $node = $xml->xpath('//item');

   foreach ($node as $n) {               
       // Iterate through each child of <item> node
       foreach ($columns as $col) {         
           if (count($xml->xpath('//item['.$i.']/'.$col)) > 0) {
              $values[] = trim($xml->xpath('//item['.$i.']/'.$col)[0]);
           } else {
              $values[] = '';
           }    
       }    
       // Write to CSV files (appending to column headers)
       $fs = fopen('GoogleProductFeed.csv', 'a');
       fputcsv($fs, $values);      
       fclose($fs);  

       $values = [];    // Clean out array for next <item> (i.e., row)
       $i++;            // Move to next <item> (i.e., node position)
   }
}