我有这样的代码,它读取XML文档并将其复制到不同格式的XML。
$new = '<prestashop>';
while ($xml_reader->read() and $xml_reader->name !== 'product');
while ($xml_reader->name === 'product') // dla kazdego produktu
{
$node = new SimpleXMLElement($xml_reader->readOuterXML());
$new .= '<product>';
foreach ($this->columns as $out => $in) // dla kazdej kolumny xml
{
if ($node->$xml !== '') // jesli ma wartosc
{
$new .= "<{$out}>{$node->$in}</{$out}>";
}
else
{
$new .= "<{$out}/>";
}
}
$new .= '</product>';
}
$new .= '</prestashop>';
XML具有这样的结构:<product>...</product><product>...</product>
。我检查了所有我可以和错误可能是在while
。
@edit:我使用PHP的XML Reader逐个获取节点,然后SimpleXML处理节点本身。
这是愚蠢的。我忘了$xml_reader->next('product');
,所以while
循环总是绕着同一个节点转。