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

© . All rights reserved.