【发布时间】:2014-12-16 01:24:13
【问题描述】:
我有一个 xml 文件,我希望将其转换为 csv 文件。我的 xml 文件是具有产品编号和两种属性的产品列表
attrtype=ProductattributeTypeTechDataattrtype=ProductattributeTypeScopeSupply
我设法获得了所有TypeTechData 属性,因为它应该全部放在一个文件中,但我无法弄清楚如何执行TypeScopeSupply。这是 XML 文件
<PRV proid="96801" id="96802" name="1.601-555.0 K 2.080">
<PRODUCT_NUMBER>16015550</PRODUCT_NUMBER>
<COLL languagespecific="" attrtype="ProductattributeTypeTechData" ProdID="96802" name="SPANNUNG_COLL" product_number="16015550" dictionary_entry="Voltage" dict_id="1775">
<PRAT languagespecific="0" attrtype="ProductattributeTypeTechData" datatype="N" ProdID="96802" name="SPANNUNG_VON" product_number="16015550">
<VALUE nr="1" unit="V" unit_id="3209" vo="" vo_id="">220</VALUE>
</PRAT>
</COLL>
<PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="96802" name="INTEGRIERTER_FEINWASSERFILTER" product_number="16015550" dictionary_entry="Integrated fine mesh water filter" dict_id="2258">
<VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
</PRAT>
<PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="96802" name="ADAPTER_GARTENSCHLAUCHANSCHLUSS_A3_4" product_number="16015550" dictionary_entry="Garden hose adapter A3/4"" dict_id="2113">
<VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
</PRAT>
<COLL languagespecific="" attrtype="ProductattributeTypeScopeSupply" ProdID="96802" name="QUICK_CONNECT_DUESEN_COLL" product_number="16015550" dictionary_entry="Kärcher Quick Connect standard nozzles" dict_id="44501">
<PRAT languagespecific="" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="96802" name="QUCK_CONNECT_DUESEN_0" product_number="16015550"/>
</COLL>
</PRV>
<PRV proid="188613" id="422136" name="1.602-103.0 K 2.090">
<PRODUCT_NUMBER>16021030</PRODUCT_NUMBER>
<COLL languagespecific="" attrtype="ProductattributeTypeTechData" ProdID="422136" name="SPANNUNG_COLL" product_number="16021030" dictionary_entry="Voltage" dict_id="1775">
<PRAT languagespecific="0" attrtype="ProductattributeTypeTechData" datatype="N" ProdID="422136" name="SPANNUNG_VON" product_number="16021030">
<VALUE nr="1" unit="V" unit_id="3209" vo="" vo_id="">220</VALUE>
</PRAT>
</COLL>
<PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="INTEGRIERTER_FEINWASSERFILTER" product_number="16021030" dictionary_entry="Integrated fine mesh water filter" dict_id="2258">
<VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
</PRAT>
<PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="ADAPTER_GARTENSCHLAUCHANSCHLUSS_A3_4" product_number="16021030" dictionary_entry="Garden hose adapter A3/4"" dict_id="2113">
<VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
</PRAT>
<PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="HOCHDRUCKPISTOLE" product_number="16021030" dictionary_entry="High-pressure gun" dict_id="2173">
<VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
</PRAT>
<PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="S" ProdID="422136" name="REINIGUNGSMITTELSCHLAUCH" product_number="16021030" dictionary_entry="Cleaning agent hose" dict_id="2344">
<VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">With filter</VALUE>
</PRAT>
<PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="EIN_FACH_STRAHLROHR" product_number="16021030" dictionary_entry="Single spray lance" dict_id="2247">
<VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
</PRAT>
<COLL languagespecific="" attrtype="ProductattributeTypeScopeSupply" ProdID="422136" name="QUICK_CONNECT_DUESEN_COLL" product_number="16021030" dictionary_entry="Kärcher Quick Connect standard nozzles" dict_id="44501">
<PRAT languagespecific="" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="QUCK_CONNECT_DUESEN_0" product_number="16021030"/>
</COLL>
</PRV>
我真正想要的是将每个产品保存在一个由产品 ID 命名的单独 csv 中
如果TypeScopeSupply 的数据中有任何值并且只有这些属性的标题为dictionaryentry,则仅存储TypeScopeSupply 的数据。因为有一些TypeScopeSupply 里面没有任何值或者说是空的。我不希望这些空属性被回显。
另外需要注意的是TypeScopeSupply 的值可能为COLL-PRAT-VALUE 或只是PRAT-VALUE。
即使我知道如何做到这一点,我也会很感激。我特意在这里放了一个更大的 xml 文件来显示产品属性的多样性。不同的产品有不同类型的属性。
<?php
$productfile = "Internet.xml";
//Build Product List
if (!$xml = simplexml_load_file($productfile)) {
echo "Unable to load XML file";
} else {
echo "<span style='white-space: nowrap'>";
$products = $xml->xpath('//PRV');
//Table Header
foreach ($products as $product) {
foreach ($product->COLL as $COLL) {
foreach ($COLL->PRAT as $PRAT){
if($COLL['attrtype']=="ProductattributeTypeScopeSupply"){
$headerarray[] = $COLL['dictionary_entry'];
}
};
};
foreach ($product->PRAT as $PRAT) {
if($PRAT['attrtype']=="ProductattributeTypeScopeSupply"){
$headerarray[] = $PRAT['dictionary_entry'];
}
};
};
echo "ID,";
$headerarray = array_values(array_unique($headerarray));
//Build Seperate header array for woocommerce display
$header = implode($headerarray,"|");
$header = str_replace(",","",$header);
$header = explode("|",$header);
echo implode(",",$header);
echo "<br>";
//Build Stats Array
foreach ($products as $product) {
echo($product->PRODUCT_NUMBER);
echo ",";
foreach ($product->COLL as $COLL) {
if($COLL['attrtype']=="ProductattributeTypeScopeSupply"){
$name = $COLL['dictionary_entry'];
$unit = "";
unset($values);
foreach ($COLL->PRAT as $PRAT){
foreach ($PRAT->VALUE as $VALUE) {
$values[] = (string)$VALUE . $VALUE['unit'];
};
};
$statsarray[(string)$name] = implode(array_filter($values),"|");
}
};
foreach ($product->PRAT as $PRAT) {
if($PRAT['attrtype']=="ProductattributeTypeScopeSupply"){
$name = $PRAT['dictionary_entry'];
$unit = "";
unset($values);
foreach ($PRAT->VALUE as $VALUE) {
$values[] = (string)$VALUE . $VALUE['unit'];
};
$statsarray[(string)$name] = implode(array_filter($values),"|");
}
};
//Compare against headers
$statsarray = str_replace(",",".",$statsarray);
$spreadsheetarray = array_fill(0, count($headerarray),"");
foreach ($statsarray as $description=>$value) {
$spreadsheetarray[array_search($description,$headerarray)]=$value;
};
foreach ($spreadsheetarray as $description=> $value) {
$spreadsheetarray[$description] = str_replace(',','.',$value);
//add column for woocommerce display
//$spreadsheetarray[$description] = ",".$value;
};
echo (implode($spreadsheetarray,","));
echo "<BR>";
unset($statsarray);
unset($spreadsheetarray);
};
echo "</span>";
//End XML if
};
?>
这就是我设法做到的......当我在浏览器中查看它时,它会输出单个 csv 以及空值。所有标题及其值(包括此处打印的空值) 我不知道如何避免空值及其标题并将其放入产品特定的 csv 中。
【问题讨论】:
-
因为你还没有开始编码,你可以使用
simplexml_load_file来解析你的XML,然后从构建数组开始,最后在fputcsv()上使用它。 -
它对我有用。谢谢
标签: php xml-parsing export-to-csv