PHP - xmlwriter_text() 函式



定義和用法

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

xmlwriter_text() 函式接受 XMLWriter 類的物件和表示文字的字串值,並在當前元素中寫入它。

語法

xmlwriter_text($writer, $content);

引數

序號 引數和描述
1

writer(必填)

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

2

content(必填)

這是一個字串值,表示我們需要寫入的內容。

返回值

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

PHP 版本

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

示例

以下示例演示了 xmlwriter_text() 函式的使用 -

<?php
   //Creating an XMLWriter
   $writer = new XMLWriter();
   $uri = "result.xml";

   //Opening a writer
   $writer = xmlwriter_open_uri($uri);

   //Starting the document
   xmlwriter_start_document($writer);

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

   //Setting the attribute 
   xmlwriter_write_attribute($writer, 'attr', 'test_value');

   //Adding text to the element
   xmlwriter_text($writer, 'Welcome to Tutorialspoint');  

   //Ending the element
   xmlwriter_end_element($writer);

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

這將生成以下 XML 文件 -

<?xml version="1.0"?>
<Msg attr="test_value">Welcome to Tutorialspoint</Msg>

示例

以下是此函式在面向物件風格中的示例 -

<?php
   //Creating an XMLWriter
   $writer = new XMLWriter();
   $uri = "result.xml";

   //Opening a writer
   $writer->openUri($uri);

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

   //Starting an element
   $writer->startElement('Msg');

   //Setting the attribute 
   $writer->writeAttribute('attr', 'test_value');

   //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 attr="test_value">Welcome to Tutorialspoint</Msg>

示例

以下是此函式在面向物件風格中的示例 -

<?php
   //Creating an XMLWriter
   $writer = new XMLWriter();
   $uri = "result.xml";

   //Opening a writer
   $writer->openUri($uri);

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

   //Starting an element
   $writer->startElement('Msg');

   //Setting the attribute 
   $writer->writeAttribute('attr', 'test_value');

   //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 attr="test_value">Welcome to Tutorialspoint</Msg>
php_function_reference.htm
廣告