CakeFest 2024: The Official CakePHP Conference

DOMDocumentFragment::appendXML

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

DOMDocumentFragment::appendXMLHam bir XML verisi ekler

Açıklama

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

Bir DOMDocumentFragment nesnesine ham bir XML verisi ekler.

Bu yöntem DOM standardının bir parçası değildir. Bir DOMDocument nesnesine bir XML belge parçası eklemek için basit bir yaklaşım olarak gerçeklenmiştir.

Standardla tam uyum içinde olmak isterseniz, sahte bir kök elemana sahip geçici bir DOMDocument oluşturup XML verinizi ekleyeceğiniz kök elemanın çocuk düğümleriyle bir döngüye girmelisiniz.

Bağımsız Değişkenler

data

Eklenecek XML veri.

Dönen Değerler

Başarı durumunda true, başarısızlık durumunda false döner.

Örnekler

Örnek 1 - Belgeye XML veri eklemek

<?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();
?>

Yukarıdaki örneğin çıktısı:

<?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
16 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.
1 year ago
Strings such as " ' & < > may need to be escaped in advance.
In that case, you can use htmlspecialchars().
To Top