Answer the question
In order to leave comments, you need to log in
Can you provide a simple example for this recommendation from the official php.net site?
Please give the simplest example of this phrase-description from the official site :
...you need to create a temporary DOMDocument with a dummy root, and then iterate over all descendant nodes of the root node of the XML document to add them.
Answer the question
In order to leave comments, you need to log in
As I understand it, the author wants to know how to do it according to the standard without using the DocumentFragment::appendXML hack.
For example, like this:
$targetDoc = new DOMDocument();
$targetDoc->loadXML("<root/>");
$fragment = $targetDoc->childNodes[0]; // нода, в которую нужно загрузить XML
$doc = new DOMDocument(); // фиктивный документ, в который загружаем данные
$doc->loadXML("<root><foo>text</foo><bar>text2</bar></root>"); // оборачиваем их в фиктивного рута
// переносим
foreach ($doc->childNodes[0]->childNodes as $node) {
$clone = $targetDoc->importNode($node, true);
$fragment->appendChild($clone);
}
var_dump($doc->saveXML() == $targetDoc->saveXML());
In my opinion, this is for manipulating XML structures
we create a temporary DOMDocument?
$doc = new DOMDocument(); // инициализация
$doc->formatOutput = true; // мы хотим красивый вывод
$doc->loadXML("<root/>"); // наш XML обьект
$f = $doc->createDocumentFragment(); // создаем временный DOMDocument
$f->appendXML("<foo>text</foo><bar>text2</bar>"); // объявляем новый элемент $f
$doc->documentElement->appendChild($f); // добавляем элемент $f
echo $doc->saveXML(); // выводим на экран
echo $doc->saveXML() . "\n"; // Сохранение всего документа
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question