Ich habe es jetzt mit verschiedenen Quellcode ausprobiert. Zb. https://codelucky.com/php-dom/ Ich habe immer das Problem sobald ich ein neuer Element einfüge wird alles danach auf einer Linie Dargestellt und nicht mehr als Baumstruktur
formaOutput hat auch nichts bewirkt!
Hat jemand eine Idee wo der Fehler liegt bzw was ich anders machen kann ?
Danke
Code:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book> <title>PHP Mastery</title> <author>John Doe</author> <price>29.99</price> </book> <book> <title>XML Essentials</title> <author>Jane Smith</author> <price>24.95</price> </book> <book><title>DOM Manipulation</title><author>Alice Johnson</author><price>34.99</price></book></bookstore>
PHP-Code:
<?php
//https://codelucky.com/php-dom/
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title>PHP Mastery</title>
<author>John Doe</author>
<price>29.99
</price>
</book>
<book>
<title>XML Essentials</title>
<author>Jane Smith</author>
<price>24.95</price>
</book>
</bookstore>';
?>
<?php
$dom = new DOMDocument();
$dom->loadXML($xml);
//$dom->formatOutput = true;
// Create new elements
$newBook = $dom->createElement('book');
$newTitle = $dom->createElement('title', 'DOM Manipulation');
$newAuthor = $dom->createElement('author', 'Alice Johnson');
$newPrice = $dom->createElement('price', '34.99');
// Append new elements to the new book
$newBook->appendChild($newTitle);
$newBook->appendChild($newAuthor);
$newBook->appendChild($newPrice);
// Append the new book to the bookstore
$bookstore = $dom->getElementsByTagName('bookstore')->item(0);
$bookstore->appendChild($newBook);
// Output the updated XML
//$dom->formatOutput = true;
echo $dom->saveXML();
?>
Hat jemand eine Idee wo der Fehler liegt bzw was ich anders machen kann ?
Danke