I have this type of XML file
我有这种类型的XML文件。
Example of file part:
部分文件的例子:
<!-- language: lang-xml -->
<ponudba podjetje="SO d.o.o." velja_od="23.09.2012 @ 12:30:48">
<artikel koda="LS593EAR" naziv="HP ENVY 17-2199e" kategorija="Prenosniki" podkategorija="Hewlett Packard (HP)" v_akciji="ne" kosovnost="več">
<opis>
HP ENVY 17-2199el, Intel Core i7-2630QM (2.0 GHz), 17.3'' FHD AG LED 3D, 8 GB DDR3 (2x 4 GB), 1 TB, BluRay, ATI Radeon HD6850 1024 MB, WiFi, Bluetooth, Webcam, 3D glasses, Microsoft Windows 7 Home Premium (64 bit)
</opis>
<opis_detail>
HP ENVY 17-2199el, Intel Core i7-2630QM (2.0 GHz), 17.3'' FHD AG LED 3D, 8 GB DDR3 (2x 4 GB), 1 TB, BluRay, ATI Radeon HD6850 1024 MB, WiFi, Bluetooth, Webcam, 3D glasses, Microsoft Windows 7 Home Premium (64 bit)<br/><table> <col width="25%" /> <col /> <tbody> <tr> <th>Procesor</th> <td>Intel® Core™ i7-2630QM / 2.00 GHz / Quad-Core</td> </tr> <tr> <th>Delovni pomnilnik</th> <td>8 GB DDR3</td> </tr> <tr> <th>Trdi disk</th> <td>1 TB (1000 GB) / 5400 / SATA</td> </tr> <tr> <th>LCD zaslon</th> <td>43,9 cm (17,3'') Full HD HP Ultra BrightView Infinity Display (1920x1080)</td> </tr> <tr> <th>Grafična kartica</th> <td>AMD Radeon™ HD 6850 Graphics</td> </tr> <tr> <th>Optična enota</th> <td>SuperMulti DVD-RW Double Layer</td> </tr> <tr> <th>USB 2.0</th> <td>2x</td> </tr> <tr> <th>USB 3.0</th> <td>1x</td> </tr> <tr> <th>eSATA</th> <td>da</td> </tr> <tr> <th>HDMI</th> <td>da</td> </tr> <tr> <th>WiFi</th> <td>da</td> </tr> <tr> <th>Bluetooth</th> <td>da</td> </tr> <tr> <th>WWAN</th> <td>ne</td> </tr> <tr> <th>Spletna kamera</th> <td>da</td> </tr> <tr> <th>Card Reader</th> <td>da</td> </tr> <tr> <th>Express Card</th> <td>ne</td> </tr> <tr> <th>TV kartica</th> <td>ne</td> </tr> <tr> <th>Finger Print</th> <td>ne</td> </tr> <tr> <th>Vhodne naprave</th> <td>brez</td> </tr> <tr> <th>Operacijski sistem</th> <td>Microsoft Windows 7 Home Premium (64 bit)</td> </tr> <tr> <th>Država uvoza</th> <td>Italijanska tipkovnica (priložene SLO nalepke)</td> </tr> <tr> <th>Stanje modela</th> <td>HP Renew</td> </tr> </tbody> </table>
</opis_detail>
<garancija_v_mesecih>12</garancija_v_mesecih>
<cena_v_EUR>1.049,00</cena_v_EUR>
<proizvajalec>HP</proizvajalec>
<stanje>na zalogi</stanje>
<url_foto_artikla>
http://www.so-doo.si/media/catalog/product/cache/1/image/265x/9df78eab33525d08d6e5fb8d27136e95/c/0/c02034964.jpg.hri_4.jpg
</url_foto_artikla>
<vec_fotk_artikla>
<slika href="http://www.so-doo.si/media/catalog/product/c/0/c02034982.jpg.hri_4.jpg"/>
<slika href="http://www.so-doo.si/media/catalog/product/c/0/c02034991.jpg.hri_4.jpg"/>
</vec_fotk_artikla>
<teza_artikla_v_kg>2.9000</teza_artikla_v_kg>
</artikel>
Now I try to convert this XML file into CSV using this type of code:
现在,我尝试使用这种类型的代码将这个XML文件转换为CSV:
<?php
$filexml='so_feed.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('sofeed.csv', 'w');
foreach ($xml->naziv as $naziv) {
fputcsv($f, get_object_vars($naziv),',','"');
}
fclose($f);
}
?>`
where so_feed.xml is input XML file and sofeed.csv is output CSV file, and field "naziv" is node for which I need data.
so_feed的地方。xml是输入xml文件和sofeed。csv是输出csv文件,字段“naziv”是我需要数据的节点。
But I get only empty csv file. Some help, pls? :) Also when I use "artikel" node, i dont get whole info, just a part:(
但是我只有一个空的csv文件。一些帮助,请?)当我使用“artikel”节点时,我并没有得到完整的信息,只是一部分:
When using "artikel" array I get all info, except "kode" and "naziv", on english, that is "SKU" and "Product name" so you know that this is most important part and i can not get it, I only get word "Array" instead those data
在使用“artikel”数组时,我得到了所有的信息,除了“kode”和“naziv”,在英语中,这是“SKU”和“Product name”,所以你知道这是最重要的部分,我无法得到它,我只得到了“数组”这个词,而不是那些数据。
This is what my output CSV looks like :
这是我的输出CSV的样子:
<!-- language: lang-csv -->
Array,"HP ENVY 17-2199el, Intel Core i7-2630QM (2.0 GHz), 17.3'' FHD AG LED 3D, 8 GB DDR3 (2x 4 GB), 1 TB, BluRay, ATI Radeon HD6850 1024 MB, WiFi, Bluetooth, Webcam, 3D glasses, Microsoft Windows 7 Home Premium (64 bit)","HP ENVY 17-2199el, Intel Core i7-2630QM (2.0 GHz), 17.3'' FHD AG LED 3D, 8 GB DDR3 (2x 4 GB), 1 TB, BluRay, ATI Radeon HD6850 1024 MB, WiFi, Bluetooth, Webcam, 3D glasses, Microsoft Windows 7 Home Premium (64 bit)<br/><table>
<col width=""25%"" />
<col />
<tbody>
<tr>
<th>Procesor</th>
<td>Intel® Core™ i7-2630QM / 2.00 GHz / Quad-Core</td>
</tr>
<tr>
<th>Delovni pomnilnik</th>
<td>8 GB DDR3</td>
</tr>
<tr>
<th>Trdi disk</th>
<td>1 TB (1000 GB) / 5400 / SATA</td>
</tr>
<tr>
<th>LCD zaslon</th>
<td>43,9 cm (17,3'') Full HD HP Ultra BrightView Infinity Display (1920x1080)</td>
</tr>
<tr>
<th>Grafična kartica</th>
<td>AMD Radeon™ HD 6850 Graphics</td>
</tr>
<tr>
<th>Optična enota</th>
<td>SuperMulti DVD-RW Double Layer</td>
</tr>
<tr>
<th>USB 2.0</th>
<td>2x</td>
</tr>
<tr>
<th>USB 3.0</th>
<td>1x</td>
</tr>
<tr>
<th>eSATA</th>
<td>da</td>
</tr>
<tr>
<th>HDMI</th>
<td>da</td>
</tr>
<tr>
<th>WiFi</th>
<td>da</td>
</tr>
<tr>
<th>Bluetooth</th>
<td>da</td>
</tr>
<tr>
<th>WWAN</th>
<td>ne</td>
</tr>
<tr>
<th>Spletna kamera</th>
<td>da</td>
</tr>
<tr>
<th>Card Reader</th>
<td>da</td>
</tr>
<tr>
<th>Express Card</th>
<td>ne</td>
</tr>
<tr>
<th>TV kartica</th>
<td>ne</td>
</tr>
<tr>
<th>Finger Print</th>
<td>ne</td>
</tr>
<tr>
<th>Vhodne naprave</th>
<td>brez</td>
</tr>
<tr>
<th>Operacijski sistem</th>
<td>Microsoft Windows 7 Home Premium (64 bit)</td>
</tr>
<tr>
<th>Država uvoza</th>
<td>Italijanska tipkovnica (priložene SLO nalepke)</td>
</tr>
<tr>
<th>Stanje modela</th>
<td>HP Renew</td>
</tr>
</tbody>
</table>",12,"1.049,00",HP,"na zalogi",http://www.so-doo.si/media/catalog/product/cache/1/image/265x/9df78eab33525d08d6e5fb8d27136e95/c/0/c02034964.jpg.hri_4.jpg,,2.9000
After applying code:
在应用代码:
<!-- language: lang-php -->
<?php
// The order here determines the order in the output CSV file
$columns = array(
'koda',
'naziv',
'kategorija',
'podkategorija',
'v_akciji',
'kosovnost'
);
// This will be used later on to correctly sort in the attribute values
// Note: the third paramter of "array_fill" determines what value to use
// in case a node lacks an attribute
$csv_blueprint = array_combine(
$columns,
array_fill(0, count($columns), '')
);
$data = array($columns);
$filexml = 'so_feed.xml';
if ( !file_exists($filexml) ) {
// Do some error routine
} else {
$xml = simplexml_load_file($filexml);
$artikel = $xml->artikel;
if ( !count($artikel) ) {
// Stop processing 'cause there's nothing to do
} else {
foreach ( $artikel as $item )
{
// Clone the row blueprint to leave the original unspoiled
$row = $csv_blueprint;
$attr = $item->attributes();
foreach ( $attr as $key => $value ) {
$row[$key] = (string) $value;
}
// Append the current row to the overall output data but
// be sure to strip off the indexes and pass a numerical array
$data[] = array_values($row);
}
// The rest is up to you ... do whatever you need to :D
var_dump($data);
}
}
?>
I got only few fields output, defined in code:
我只得到了几个字段输出,在代码中定义:
<!-- language: lang-php -->
$columns = array(
'koda',
'naziv',
'kategorija',
'podkategorija',
'v_akciji',
'kosovnost'
How to get all other attributes output in csv?
如何在csv中获得所有其他属性输出?
1 个解决方案
#1
0
Based on your comments, this is what I came up with. I've tried to be very verbose to help you understand what's going on.
根据你的评论,这是我想到的。我试着非常啰嗦来帮助你理解发生了什么。
// The order here determines the order in the output CSV file
$columns = array(
'koda',
'naziv',
'kategorija',
'podkategorija',
'v_akciji',
'kosovnost'
);
// This will be used later on to correctly sort in the attribute values
// Note: the third paramter of "array_fill" determines what value to use
// in case a node lacks an attribute
$csv_blueprint = array_combine(
$columns,
array_fill(0, count($columns), '')
);
$data = array($columns);
$filexml = 'so_feed.xml';
if ( !file_exists($filexml) ) {
// Do some error routine
} else {
$xml = simplexml_load_file($filexml);
$artikel = $xml->artikel;
if ( !count($artikel) ) {
// Stop processing 'cause there's nothing to do
} else {
foreach ( $artikel as $item ) {
// Clone the row blueprint to leave the original unspoiled
$row = $csv_blueprint;
$attr = $item->attributes();
foreach ( $attr as $key => $value ) {
$row[$key] = (string) $value;
}
// Append the current row to the overall output data but
// be sure to strip off the indexes and pass a numerical array
$data[] = array_values($row);
}
// The rest is up to you ... do whatever you need to :D
var_dump($data);
}
}
EDIT
Extending the former code to reflect the need for additional columns.
编辑扩展前代码以反映额外列的需要。
-
Extend the
$columns
array the way you want to incorporate the necessary data (e.g. opis)将$columns数组扩展到您想要合并所需数据的方式(例如opis)
$columns = array( 'koda', 'naziv', 'kategorija', 'opis', // Arbitrarily added an extra column here 'podkategorija', 'v_akciji', 'kosovnost' );
-
Iterate over
<ponudba/>
instead ofartikel
迭代
而不是artikel。 $xml = simplexml_load_file($filexml); //$artikel = $xml->artikel; $ponudbas = $xml->ponudba; ... foreach ( $ponudbas as $ponudba ) { // Clone the row blueprint to leave the original unspoiled $row = $csv_blueprint;
-
Add the attributes of any
artikel
node just like before像以前一样添加任何artikel节点的属性。
- Additionally iterate over the added columns (those that correspond to nodes) and save their value within the
$row[NODE_NAME / COLUMN NAME]
bucket - 另外,迭代添加的列(与节点对应的列),并将其值保存在$row[NODE_NAME /列名]bucket中。
#1
0
Based on your comments, this is what I came up with. I've tried to be very verbose to help you understand what's going on.
根据你的评论,这是我想到的。我试着非常啰嗦来帮助你理解发生了什么。
// The order here determines the order in the output CSV file
$columns = array(
'koda',
'naziv',
'kategorija',
'podkategorija',
'v_akciji',
'kosovnost'
);
// This will be used later on to correctly sort in the attribute values
// Note: the third paramter of "array_fill" determines what value to use
// in case a node lacks an attribute
$csv_blueprint = array_combine(
$columns,
array_fill(0, count($columns), '')
);
$data = array($columns);
$filexml = 'so_feed.xml';
if ( !file_exists($filexml) ) {
// Do some error routine
} else {
$xml = simplexml_load_file($filexml);
$artikel = $xml->artikel;
if ( !count($artikel) ) {
// Stop processing 'cause there's nothing to do
} else {
foreach ( $artikel as $item ) {
// Clone the row blueprint to leave the original unspoiled
$row = $csv_blueprint;
$attr = $item->attributes();
foreach ( $attr as $key => $value ) {
$row[$key] = (string) $value;
}
// Append the current row to the overall output data but
// be sure to strip off the indexes and pass a numerical array
$data[] = array_values($row);
}
// The rest is up to you ... do whatever you need to :D
var_dump($data);
}
}
EDIT
Extending the former code to reflect the need for additional columns.
编辑扩展前代码以反映额外列的需要。
-
Extend the
$columns
array the way you want to incorporate the necessary data (e.g. opis)将$columns数组扩展到您想要合并所需数据的方式(例如opis)
$columns = array( 'koda', 'naziv', 'kategorija', 'opis', // Arbitrarily added an extra column here 'podkategorija', 'v_akciji', 'kosovnost' );
-
Iterate over
<ponudba/>
instead ofartikel
迭代
而不是artikel。 $xml = simplexml_load_file($filexml); //$artikel = $xml->artikel; $ponudbas = $xml->ponudba; ... foreach ( $ponudbas as $ponudba ) { // Clone the row blueprint to leave the original unspoiled $row = $csv_blueprint;
-
Add the attributes of any
artikel
node just like before像以前一样添加任何artikel节点的属性。
- Additionally iterate over the added columns (those that correspond to nodes) and save their value within the
$row[NODE_NAME / COLUMN NAME]
bucket - 另外,迭代添加的列(与节点对应的列),并将其值保存在$row[NODE_NAME /列名]bucket中。