PHP - xmlwriter_write_comment() 函式



定義和用法

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

xmlwriter_write_comment() 函式接受 XMLWriter 類的物件和表示註釋內容的字串值,並建立完整的註釋標籤。

語法

xmlwriter_start_comment($writer);

引數

序號 引數及描述
1

writer(必填)

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

2

comment(必填)

這是一個字串值,表示註釋的內容。

返回值

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

PHP 版本

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

示例

以下示例演示了 xmlwriter_write_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');
    
   //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);
?>

這將生成以下 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');

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

這將生成以下 XML 文件 -

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