PHP - XMLReader::lookupNamespace() 函式



定義和用法

XML 是一種標記語言,用於在網路上共享資料,XML 既可供人類閱讀,也可供機器閱讀。XMLReader 擴充套件用於讀取/檢索 XML 文件的內容,即使用 XMLReader 類的 方法可以讀取 XML 文件的每個節點。

XMLReader::lookupNamespace() 函式是 XMLReader 類的一個函式,它接受一個表示名稱空間字首的字串值,並在作用域名稱空間中查詢給定的字首。

語法

XMLReader::lookupNamespace($prefix);

引數

序號 引數及說明
1

prefix(必填)

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

返回值

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

PHP 版本

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

示例

以下示例演示了 XMLReader::lookupNamespace() 函式的用法 -

data.xml

<?xml version="1.0" encoding="utf-8"?> 
<Employee xmlns:ns="testnamespace">
   <ns:Name ns:id = "name">Krishna</ns:Name>
   <ns:Age ns:id = "age">22</ns:Age>
   <ns:City ns:id = "city">Hyderabad</ns:City>   
   <ns:Phone ns:id = "phone">980000000</ns:Phone>   
</Employee>

sample.php

<?php
   //Creating an XMLReader
   $reader = new XMLReader();

   //Opening a reader
   $reader->open('trail.xml');

   //reading the contents of the node
   $reader->read();
   $res = $reader->lookupNamespace("ns"); 
   print("Name space: ".$res);
   
   //Closing the reader
   $reader->close();
?>

這將產生以下結果 -

Name space: testnamespace

示例

以下是此函式的另一個示例 -

<?php
   //Creating an XMLReader
   $reader = new XMLReader();

   $data = "<data xmlns:ns='testnamespace'> 
      <ns:name ns:att = 'test_attribute'>Raju</ns:name> 
      <age>32</age> 
      <phone>9848022338</phone> 
      <city>Hyderabad</city>
   </data>";

   //Opening a reader
   $reader->xml($data);

   //reading the contents of the node
   $reader->read();
   $res = $reader->lookupNamespace("ns"); 
   print("Name space: ".$res);

   //Closing the reader
   $reader->close();

   //Closing the reader
   $reader->close();
?>

這將產生以下結果 -

Name space: testnamespace
php_function_reference.htm
廣告