PHP - xmlwriter_write_pi() 函数



定义和用法

XML 是一种标记语言,用于在网络上共享数据。XML 既可供人类阅读,也可供机器阅读。XMLWriter 扩展内部使用 libxml xmlWriter API,用于编写/创建 XML 文档的内容。由此生成的 XML 文档是非缓存的,并且是单向的。

xmlwriter_write_pi() 函数接受 XMLWriter 类的对象以及表示处理指令的目标及其值的两个字符串值作为参数,并创建一个完整的 PI 标签。

语法

xmlwriter_end_pi($writer);

参数

序号 参数 & 说明
1

writer (必填)

这是 XMLWriter 类的对象,表示您想要修改/创建的 XML 文档。

2

str (必填)

这是一个字符串值,表示处理指令的目标。

3

content (必填)

这是一个字符串值,表示 PI 指令的内容。

返回值

此函数返回一个布尔值,成功时为 TRUE,失败时为 FALSE。

此函数首次在 PHP 5 版本中引入,并在所有后续版本中均有效。

示例

以下示例演示了 xmlwriter_write_pi() 函数的用法:

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

   //Setting the indentation on
   xmlwriter_set_indent($writer, TRUE);

   //Starting the document
   xmlwriter_start_document($writer);
   xmlwriter_set_indent_string($writer, "  ");

   //Creating the PI tag
   xmlwriter_write_pi($writer, 'php', 'echo $a;');

   //Setting the indentation
   xmlwriter_set_indent_string($writer, "    ");

   //Starting an element
   xmlwriter_start_element($writer, 'Tutorial');
   xmlwriter_start_element($writer, 'name');
   
   //Adding text to the element
   xmlwriter_text($writer, 'JavaFX');  
   xmlwriter_end_element($writer);
   xmlwriter_start_element($writer, 'Author');
   
   //Adding text to the element
   xmlwriter_text($writer, 'Krishna');  
   xmlwriter_end_element($writer);

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

这将生成以下 XML 文档:

<?xml version="1.0"?>
<!ATTLIST sample_dtd_list>
<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();

   //Setting the indentation on
   $writer->setIndent(TRUE);
   
   //Setting the indentation
   $writer->setIndentString("    ");

   //Starting the Dtd Attlist tag
   $writer->startDtdAttlist('sample_dtd_list');

   //Ending the Dtd Attlist tag
   $writer->endDtdAttlist(); 

   //Starting an element
   $writer->startElement('Tutorial');
   $writer->startElement('name');
   
   //Adding text to the element
   $writer->text('JavaFX');  
   $writer->endElement();
   $writer->startElement('Author');
   
   //Adding text to the element
   $writer->text('Krishna');  
   $writer->endElement();

   //Ending the element
   $writer->endElement();
   
   //Ending the document
   $writer->endDocument();
?> 

这将生成以下 XML 文档:

<?xml version="1.0"?>
<!ATTLIST sample_dtd_list>
<Tutorial>
   <name>JavaFX</name>
   <Author>Krishna</Author>
</Tutorial>
php_function_reference.htm
广告