【发布时间】:2015-06-04 22:30:16
【问题描述】:
大家好,我想知道是否有一种快速的方法来获取 xml family->family['code'] 并查看它是否等于 products->product->family['code'] 以及是否相等用family->family['title']替换product->family['code']。
xml 代码:
<products>
<product code="002732" title="Bluetooth Headset " price="2.00" available="true">
<family code="03" />
<description>
<lang-en><![CDATA[This product reproduces the original artwork]]></lang-en>
</description>
</product>
<product code="004587" title="Headset " price="5.00" available="true">
<family code="05" />
<description>
<lang-en><![CDATA[something]]></lang-en>
</description>
</product>
</products>
<families>
<family code="03" title="Mobile Accessories"></family>
<family code="05" title="Accessories"></family>
</families>
我的php代码:
$fileName = 'demo.csv';
$xml=simplexml_load_file("demo.xml") or die("Error: Cannot create object");
ob_start();
echo 'Product code;Language;Product name;Price;Status;Detailed image;Description;Category'."\n";
foreach($xml->products->product as $child){
echo $child['code'].';en;';
echo $child['title'].';';
echo $child['price'].';';
echo $child['available'].';';
echo $child->image['src'].';';
echo $child->description->{'lang-en'}."\n";;
}
$htmlStr = ob_get_contents();
ob_end_clean();
file_put_contents($fileName, $htmlStr);
【问题讨论】: