PHP - xmlwriter_write_attribute_ns() 函式



定義和用法

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

xmlwriter_write_attribute_ns() 函式用於建立完整的名稱空間屬性標籤。

語法

xmlwriter_write_attribute_ns($writer, $prefix, $name, $uri, $value);

引數

序號 引數和描述
1

writer(必填)

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

2

Prefix(必填)

這是表示名稱空間字首的字串值。

3

name(必填)

這是表示屬性名稱的字串值。

4

uri(必填)

這是指定名稱空間 uri 的字串值。

5

content(必填)

這是表示屬性值的字串。

返回值

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

PHP 版本

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

示例

以下示例演示了 xmlwriter_write_attribute_ns() 函式的用法:

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

   //Creating the namespaced attribute
   xmlwriter_write_attribute_ns($writer, 'ns', 'attr', 'test.uri', '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 ns:attr="test_value" xmlns:ns="test.uri">Welcome to Tutorialspoint</Msg>

示例

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

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

   //Creating the namespaced attribute
   $writer->writeAttributeNs('ns', 'attr', 'test.uri', '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 ns:attr="test_value" xmlns:ns="test.uri">Welcome to Tutorialspoint</Msg>
php_function_reference.htm
廣告