PHP - Función xmlwriter_write_comment ()

Definición y uso

XML es un lenguaje de marcado para compartir los datos a través de la web, XML es legible tanto para humanos como para máquinas. La extensión XMLWriter tiene internamente la API libxml xmlWriter y se usa para escribir / crear el contenido de un documento XML. Los documentos XML generados por esto no se almacenan en caché y solo se reenvían.

los xmlwriter_write_comment() La función acepta un objeto de la clase XMLWriter y un valor de cadena que representa el contenido de un comentario y crea una etiqueta de comentario completa.

Sintaxis

xmlwriter_start_comment($writer);

Parámetros

No Señor Descripción de parámetros
1

writer(Mandatory)

Este es un objeto de la clase XMLWriter que representa el documento XML que desea modificar / crear.

2

comment(Mandatory)

Este es un valor de cadena que representa el contenido de un comentario.

Valores devueltos

Esta función devuelve un valor booleano que es VERDADERO en caso de éxito y FALSO en caso de falla.

Versión PHP

Esta función se introdujo por primera vez en PHP Versión 5 y funciona en todas las versiones posteriores.

Ejemplo

El siguiente ejemplo demuestra el uso de xmlwriter_write_comment() función -

<?php
   //Creating an XMLWriter
   $writer = new XMLWriter();

   //Opening a writer
   $uri = "result.xml";
   $writer = xmlwriter_open_uri($uri);

   //Starting the document
   xmlwriter_start_document($writer);

   //Starting an element
   xmlwriter_start_element($writer, 'Msg');
    
   //Creating a comment tag
   xmlwriter_write_comment($writer, 'This is a sample comment' );

   //Adding text to the element
   xmlwriter_text($writer, 'Welcome to Tutorialspoint');  

   //Starting an element
   xmlwriter_end_element($writer);

   //Ending the document
   xmlwriter_end_document($writer);
?>

Esto generará el siguiente documento XML:

<?xml version="1.0"?>
<Msg><!--This is a sample comment-->Welcome to Tutorialspoint</Msg>

Ejemplo

A continuación se muestra el ejemplo de esta función en estilo orientado a objetos:

<?php
   //Creating an XMLWriter
   $writer = new XMLWriter();

   //Opening a writer
   $uri = "result.xml";
   $writer->openUri($uri);

   //Starting the document
   $writer->startDocument();

   //Starting an element
   $writer->startElement('Msg');

   //Creating the comment tag 
   $writer->writeComment('This is a sample comment'); 

   //Adding text to the element
   $writer->text('Welcome to Tutorialspoint');  

   //Ending the element
   $writer->endElement();

   //Ending the document
   $writer->endDocument();
?>

Esto generará el siguiente documento XML:

<?xml version="1.0"?>
<Msg><!--This is a sample comment-->Welcome to Tutorialspoint</Msg>