PHP | DOMDocument xinclude() Function
Last Updated :
26 Feb, 2020
The DOMDocument::xinclude() function is an inbuilt function in PHP which is used to substitute the XIncludes in a DOMDocument Object.
Syntax:
int DOMDocument::xinclude( int $options = 0 )
Parameters: This function accepts an optional single parameter $options which holds the libxml parameter.
Return Value: This function returns the number of XIncludes in the document, -1 if some processing failed, or FALSE if there were no substitutions.
Below examples illustrate the DOMDocument::xinclude() function in PHP:
Example 1: In this program, we will include from XML from new.xml to index.php.
Example 2: In this program, we will add a fallback if xinclude fails.
- index.php
<?php
$xml = <<<EOD
<?xml version= "1.0" ?>
<xi: include href= "wrong_name.xml" >
<xi:fallback>
<error>xml not found</error>
</xi:fallback>
</xi: include >
</root>
EOD;
$dom = new DOMDocument();
$dom ->preserveWhiteSpace = false;
$dom ->formatOutput = true;
$dom ->loadXML( $xml );
$dom ->xinclude();
echo $dom ->saveXML();
?>
|
- Output:
xml not found
Reference: https://www.php.net/manual/en/domdocument.xinclude.php
Share your thoughts in the comments
Please Login to comment...