PHP - Fonction xmlwriter_write_comment ()

Définition et utilisation

XML est un langage de balisage permettant de partager les données sur le Web, XML est à la fois lisible par l'homme et par machine. L'extension XMLWriter a en interne l'API libxml xmlWriter et est utilisée pour écrire / créer le contenu d'un document XML. Les documents XML générés par celui-ci ne sont pas mis en cache et uniquement en avant.

le xmlwriter_write_comment() La fonction accepte un objet de la classe XMLWriter et une valeur de chaîne représentant le contenu d'un commentaire et crée une balise de commentaire complète.

Syntaxe

xmlwriter_start_comment($writer);

Paramètres

Sr. Non Paramètre et description
1

writer(Mandatory)

Il s'agit d'un objet de la classe XMLWriter représentant le document XML que vous souhaitez modifier / créer.

2

comment(Mandatory)

Il s'agit d'une valeur de chaîne représentant le contenu d'un commentaire.

Valeurs de retour

Cette fonction renvoie une valeur booléenne qui est TRUE en cas de succès et FALSE en cas d'échec.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation du xmlwriter_write_comment() fonction -

<?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);
?>

Cela générera le document XML suivant -

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

Exemple

Voici l'exemple de cette fonction dans le style orienté objet -

<?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();
?>

Cela générera le document XML suivant -

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