PHP - xmlwriter_flush() 函式



定義和用法

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

xmlwriter_flush() 函式接受 XMLWriter 類的物件作為引數,並重新整理當前緩衝區。

語法

xmlwriter_flush($xmlwriter, $bool);

引數

序號 引數和描述
1

writer(必填)

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

2

bool(可選)

這是一個布林值,指定是否清空緩衝區。

返回值

如果 writer 在記憶體中開啟,則此函式返回 XML 緩衝區;如果我們使用 URI,則返回位元組數。

PHP 版本

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

示例

下面的示例演示了 xmlwriter_flush() 函式的用法:

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

   //Starting the document
   xmlwriter_start_document($writer);

   //Creating XML elements
   xmlwriter_set_indent($writer, TRUE);
   xmlwriter_set_indent_string($writer, "    ");

   //Starting an element
   xmlwriter_start_element($writer, 'Tutorial');

   //Starting a element tag
   xmlwriter_start_element($writer, 'name');

   //Adding text to the element
   xmlwriter_text($writer, 'JavaFX');  
   xmlwriter_full_end_element($writer);
   xmlwriter_start_element($writer, 'Author');
   
   //Adding text to the element
   xmlwriter_text($writer, 'Krishna');  
   xmlwriter_full_end_element($writer);

   //Ending the element
   xmlwriter_full_end_element($writer);

   //Ending the document
   xmlwriter_full_end_element($writer);

   //Flushing the contents of the document
   xmlwriter_flush($writer, TRUE);
?> 

這將生成以下 XML 文件:

<?xml version="1.0"?>
<Tutorial>
   <name>JavaFX</name>
   <Author>Krishna</Author>
</Tutorial>

示例

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

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

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

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

   //Ending the document
   $writer->fullEndElement();

   //Flushing the contents of the XMLWriter
   $writer->flush(TRUE);
?>

這將生成以下 XML 文件:

<?xml version="1.0"?>
<Msg>Welcome to Tutorialspoint</Msg>
php_function_reference.htm
廣告