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
广告