Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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
  
// Create a new DOMDocument
$doc = new DOMDocument;
  
// Load the XML
$doc->loadXML("<root/>");
  
// Create a Document Fragment
$f = $doc->createDocumentFragment();
  
// Append the XML to fragment
$f->appendXML(
  "<h1>Heading 1</h1><strong>Strong text</strong>");
  
// Append the fragment to document
$doc->documentElement->appendChild($f);
  
// Save the XML
echo $doc->saveXML(); 
?>

Output:

<?xml version="1.0"?>
<root><h1>Heading 1</h1><strong>Strong text</strong></root>

Program 2:




<?php
  
// Create a new DOMDocument
$doc = new DOMDocument;
  
// Load the XML
$doc->loadXML("<root/>");
  
// Create a Document Fragment
$f = $doc->createDocumentFragment();
  
// Append the XML to fragment
$f->appendXML("<h1 style=\"color: red\"> Red </h1>");
$f->appendXML("<h1 style=\"color: green\"> Green </h1>");
$f->appendXML("<h1 style=\"color: blue\"> Blue </h1>");
  
// Append the fragment to document
$doc->documentElement->appendChild($f);
  
// Save the XML
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




My Personal Notes arrow_drop_up
Recommended Articles
Page :