DOMDocumentFragment::appendXML

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

DOMDocumentFragment::appendXML生の XML データを追加する

説明

public DOMDocumentFragment::appendXML(string $data): bool

生の XML データを DOMDocumentFragment に追加します。

このメソッドは、DOM の標準にはないものです。 これは、XML DocumentFragment を DOMDocument に簡単に追加できるように作成されました。

標準に従いたい場合は、まずテンポラリの DOMDocument をダミーのルートで作成し、 追加したい XML データのルートの子ノードを順にループする必要があります。

パラメータ

data

追加する XML。

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 XML データのドキュメントへの追加

<?php
$doc
= new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo
$doc->saveXML();
?>

上の例の出力は以下となります。

<?xml version="1.0"?>
<root><foo>text</foo><bar>text2</bar></root>

add a note

User Contributed Notes 2 notes

up
1
lpetrov(AT)axisvista.com
18 years ago
Here is (maybe) a better example:/** * Helper function for replacing $node (DOMNode) * with an XML code (string) * * @var DOMNode $node * @var string $xml */ public function replaceNodeXML(&$node,$xml) {  $f = $this->dom->createDocumentFragment();  $f->appendXML($xml);  $node->parentNode->replaceChild($f,$node); }Copied from the "PHP5 Dom Based Template" article at:http://blog.axisvista.com/?p=35
up
0
Thanks to Christoph.
3 years ago
Strings such as " ' & < > may need to be escaped in advance.In that case, you can use htmlspecialchars().
To Top