Strings such as " ' & < > may need to be escaped in advance.
In that case, you can use htmlspecialchars().
PHP - Manual: DOMDocumentFragment::appendXML
2024-11-13
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
DOMDocumentFragment::appendXML — Append raw XML data
$data
): boolAppends raw XML data to a DOMDocumentFragment.
This method is not part of the DOM standard. It was created as a simpler approach for appending an XML DocumentFragment in a DOMDocument.
If you want to stick to the standards, you will have to create a temporary DOMDocument with a dummy root and then loop through the child nodes of the root of your XML data to append them.
data
XML to append.
成功时返回 true
, 或者在失败时返回 false
。
示例 #1 Appending XML data to your document
<?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>
Strings such as " ' & < > may need to be escaped in advance.
In that case, you can use htmlspecialchars().
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
官方地址:https://www.php.net/manual/en/domdocumentfragment.appendxml.php