Tuesday, June 16, 2026
HomeLanguagesPHP | DOMNode C14NFile() Function

PHP | DOMNode C14NFile() Function

The DOMNode::C14NFile() function is an inbuilt function in PHP which is used to canonicalize nodes to a file.

Syntax:

int DOMNode::C14NFile( string $uri, bool $exclusive, 
bool $with_comments, array $xpath, array $ns_prefixes )

Parameters: This function accepts five parameters as mentioned above and described below:

  • $uri (Optional): It specifies the path to write the output to.
  • $exclusive (Optional): It specifies whether to enable exclusive parsing of only the nodes matched by the provided xpath or namespace prefixes.
  • $with_comments (Optional): It specifies whether to retain comments in output.
  • $xpath (Optional): It specifies an array of xpaths to filter the nodes by.
  • $ns_prefixes (Optional): It specifies an array of namespace prefixes to filter the nodes by.

Return Value: This function returns the number of bytes written or FALSE on failure

Below examples illustrate the DOMNode::C14NFile() function in PHP:

Example 1: In this example, we will save the DOM content in the form of string to a file without comments




<?php
  
// Create a DOMDocument
$doc = new DOMDocument();
   
// Load XML
$doc->loadXML('<html></html>');
   
// Create an heading element on DOMDocument object
$h1 = $doc->createElement('h1');
   
// Append the child
$doc->documentElement->appendChild($h1);
   
// Save the data without comments
$stringdata = $doc->C14NFile('new.txt');
?>


Output:This will create a new.txt file with the following text content

<html><h1></h1></html>

Example 2: In this example, we will save the DOM content in the form of string to a file with comments.




<?php
  
// Create a DOMDocument
$doc = new DOMDocument();
  
// Load XML
$doc->loadXML('<html><!-- This is a comment --></html>');
  
// Create an heading element on DOMDocument object
$h1 = $doc->createElement('h1');
  
// Append the child
$doc->documentElement->appendChild($h1);
  
// Save the data with comments
$stringdata = $doc->C14NFile('new.txt', false, true);
?>


Output: This will create a new.txt file with the following text content

<html><!-- This is a comment --><h1></h1></html>

Reference: https://www.php.net/manual/en/domnode.c14nfile.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS