products) || !isset($xml->products->product)) {
die('Błąd: Nieprawidłowa struktura XML.');
}
// **Tworzymy NOWY obiekt SimpleXMLElement, aby od początku ustawić przestrzeń nazw**
$xmlString = '' . "\n";
$xmlString .= '' . "\n";
// Iterujemy przez produkty i dodajemy poprawnie element
foreach ($xml->products->product as $product) {
$ean = (string) $product->ean;
// Usuwamy potencjalne znaczniki HTML i białe znaki
$ean = strip_tags(trim($ean));
$xmlString .= " \n";
foreach ($product->children() as $child) {
$childValue = strip_tags(trim((string) $child)); // Usuwamy HTML
$xmlString .= " <{$child->getName()}>{$childValue}{$child->getName()}>\n";
}
if (!empty($ean)) {
$xmlString .= " {$ean}\n";
}
$xmlString .= " \n";
}
$xmlString .= "\n";
// **Zapisujemy poprawny XML do pliku**
file_put_contents('feed-xml.xml', $xmlString);
echo '
wygenerowano feed-xml.xml
';
?>