PHP | DOMDocumentFragment appendXML() Function
Last Updated :
20 Feb, 2020
The DOMDocument::appendXML() function is an inbuilt function in PHP which is used to append raw XML data to a DOMDocumentFragment.
Syntax:
bool DOMDocumentFragment::appendXML( string $data )
Parameters: This function accepts a single parameter $data which holds the XML to append.
Return Value: This function returns TRUE on success or FALSE on failure.
Below given programs illustrate the DOMDocument::appendXML() function in PHP:
Program 1:
<?php
$doc = new DOMDocument;
$doc ->loadXML( "<root/>" );
$f = $doc ->createDocumentFragment();
$f ->appendXML(
"<h1>Heading 1</h1><strong>Strong text</strong>" );
$doc ->documentElement->appendChild( $f );
echo $doc ->saveXML();
?>
|
Output:
<?xml version="1.0"?>
<root><h1>Heading 1</h1><strong>Strong text</strong></root>
Program 2:
<?php
$doc = new DOMDocument;
$doc ->loadXML( "<root/>" );
$f = $doc ->createDocumentFragment();
$f ->appendXML( "<h1 style=\"color: red\"> Red </h1>" );
$f ->appendXML( "<h1 style=\"color: green\"> Green </h1>" );
$f ->appendXML( "<h1 style=\"color: blue\"> Blue </h1>" );
$doc ->documentElement->appendChild( $f );
echo $doc ->saveXML();
?>
|
Output:
<?xml version="1.0"?>
<root>
<h1 style="color: red"> Red </h1>
<h1 style="color: green"> Green </h1>
<h1 style="color: blue"> Blue </h1>
</root>
Reference: https://www.php.net/manual/en/domdocumentfragment.appendxml.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...