PHP - Fonction xmlwriter_start_pi ()

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_start_pi() accepte un objet de la classe XMLWriter et une chaîne représentant la cible de l'instruction de traitement en tant que paramètre, et crée une balise PI de début.

Syntaxe

xmlwriter_start_pi($writer, $str);

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

str(Mandatory)

Il s'agit d'une valeur de chaîne représentant la cible de l'instruction de traitement.

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_start_pi() fonction -

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

   //Setting the indentation on
   xmlwriter_set_indent($writer, TRUE);

   //Starting the document
   xmlwriter_start_document($writer);
   xmlwriter_set_indent_string($writer, "  ");

   //Starting the processing instruction
   xmlwriter_start_pi($writer, 'php');   
   
   //Write the instruction content of the processing instruction
   xmlwriter_text($writer, 'echo $a;');   
   
   //Ending the processing instruction
   xmlwriter_end_pi($writer); 

   //Setting the indentation
   xmlwriter_set_indent_string($writer, "    ");

   //Starting an element
   xmlwriter_start_element($writer, 'Tutorial');
   xmlwriter_start_element($writer, 'name');
   
   //Adding text to the element
   xmlwriter_text($writer, 'JavaFX');  
   xmlwriter_end_element($writer);
   xmlwriter_start_element($writer, 'Author');
   
   //Adding text to the element
   xmlwriter_text($writer, 'Krishna');  
   xmlwriter_end_element($writer);

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

Cela générera le document XML suivant -

<?xml version="1.0"?>
<?php echo $a;?>
<Tutorial>
   <name>JavaFX</name>
   <Author>Krishna</Author>
</Tutorial>

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

   //Setting the indentation on
   $writer->setIndent(TRUE);

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

   //Starting the processing instruction
   $writer->startPi('php');   
   
   //Write the instruction content of the processing instruction
   $writer->text('echo $a;');   
   
   //Ending the processing instruction
   $writer->endPi(); 

   //Setting the indentation
   $writer->setIndentString("    ");

   //Starting an element
   $writer->startElement('Tutorial');
   $writer->startElement('name');
   
   //Adding text to the element
   $writer->text('JavaFX');  
   $writer->endElement();
   $writer->startElement('Author');
   
   //Adding text to the element
   $writer->text('Krishna');  
   $writer->endElement();

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

Cela générera le document XML suivant -

<?xml version="1.0"?>
<?php echo $a;?>
<Tutorial>
   <name>JavaFX</name>
   <Author>Krishna</Author>
</Tutorial>