PHP - xmlwriter_start_comment() 函式



定義和用法

XML 是一種用於在網路上共享資料的標記語言,它既可讀也可機器可讀。XMLWriter 擴充套件內部具有 libxml xmlWriter API,用於編寫/建立 XML 文件的內容。由此生成的 XML 文件是非快取的且是單向的。

xmlwriter_start_comment() 函式接受 XMLWriter 類的一個物件,並開始一個註釋標籤。

語法

xmlwriter_start_comment($writer);

引數

序號 引數及描述
1

writer(必填)

這是 XMLWriter 類的一個物件,代表您想要修改/建立的 XML 文件。

返回值

此函式返回一個布林值,成功時為 TRUE,失敗時為 FALSE。

PHP 版本

此函式首次在 PHP 5 版本中引入,並在所有後續版本中可用。

示例

以下示例演示了 xmlwriter_start_comment() 函式的用法:

<?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');
    
   //Starting the comment 
   xmlwriter_start_comment($writer); 
     
   //Setting value to the comment
   xmlwriter_text($writer, 'This is a sample comment'); 
     
   //Ending the comment 
   xmlwriter_end_comment($writer); 

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

這將生成以下 XML 文件:

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

示例

以下是此函式的面向物件風格示例:

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

   //Starting the comment 
   $writer->startComment(); 
     
   //Setting value to the comment
   $writer->text('This is a sample comment'); 
     
   //Ending the comment 
   $writer->endComment(); 

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

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

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

這將生成以下 XML 文件:

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