Open In App

PHP | XMLWriter openUri() Function

Last Updated : 07 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
The XMLWriter::openUri() function is an inbuilt function in PHP which is used to create a new XMLWriter using source URI for output. In simple words, this function decides how to output the XML to user, it can be through a browser or directly to a file. Syntax:
bool XMLWriter::openUri( string $uri )
Parameters:This function accepts a single parameter $uri which holds the uri for output. Return Value: This function returns TRUE on success or FALSE on failure. Below examples illustrate the XMLWriter::openUri() function in PHP: Example 1: php
<?php

// Create a new XMLWriter instance
$writer = new XMLWriter();
 
// Create the output stream as PHP
$writer->openURI('php://output');
 
// Start the document
$writer->startDocument('1.0', 'UTF-8');
 
// Start a element
$writer->startElement('i');
 
// Add value to the element
$writer->text('GeeksforGeeks');
 
// End the element
$writer->endElement();
 
// End the document
$writer->endDocument();
?>
Output:
GeeksforGeeks
Example 2: php
<?php

// Create a new XMLWriter instance
$writer = new XMLWriter();
 
// Create the output stream to a file
$writer->openURI('new.xml');
 
// Start the document
$writer->startDocument('1.0', 'UTF-8');
 
// Start a element
$writer->startElement('div');
 
// Add value to the element
$writer->text('Hello World');
 
// End the element
$writer->endElement();
 
// End the document
$writer->endDocument();
?>
Output: This will create a new file called new.xml in the same folder with the following content

Next Article

Similar Reads