定義和用法
XML 是一種 mark-up 語言,用於在網絡上共享數據,XML 用於人類 read-able 和機器 read-able。 SimpleXMLElement 類表示 PHP 中的 XML 文檔。
這個SimpleXMLElement::getDocNamespaces()函數檢索並返回文檔中聲明的命名空間。
用法
SimpleXMLElement::getDocNamespaces([$recursive, $from_root]);
參數
Sr.No | 參數及說明 |
---|---|
1 |
recursive (Optional) 這是一個布爾值,如果傳遞 TRUE 此函數返回父節點和子節點的命名空間。 |
2 |
from_root (Optional) 如果傳遞 TRUE,則這是一個布爾值,此函數檢查子節點(而不是根節點)下的命名空間。 |
返回值
此函數返回一個包含名稱空間的數組。
PHP版本
這個函數最初是在 PHP 版本 5 中引入的,並且適用於所有後續版本。
示例
以下示例演示了 SimpleXMLElement::getDocNamespaces() 函數的用法。
<html>
<head>
<body>
<?php
$str="<?xml version='1.0' standalone='yes'?>
<Tutorial xmlns:p='http://test.org/ns'>
<Name>JavaFX</Name>
<Pages>535</Pages>
<Author>Krishna</Author>
<Version>11</Version>
</Tutorial>";
$xml = new SimpleXMLElement($str);
$result = $xml->getDocNamespaces();
print_r($result);
?>
</body>
</head>
</html>
這將產生以下結果 -
JavaFX 535 Krishna 11 600 SimpleXMLElement Object ( [@attributes] => Array ( [type] => test ) [Name] => JavaFX [Pages] => 535 [Author] => Krishna [Version] => 11 [Tutorial] => SimpleXMLElement Object ( [Price] => 600 ) )
示例
以下是帶有可選參數的此函數的示例 -
<html>
<head>
<body>
<?php
$str="<Tutorial xmlns:t='http://example.org/ns' xmlns:test='http://demo.com/test'>
<t:Name test:ns='a'>JavaFX</t:Name>
<t:Pages test:ns='b'>535</t:Pages>
<t:Author test:ns='c'>Krishna</t:Author>
<t:Version test:ns='d'>11</t:Version>
</Tutorial>";
$xml = new SimpleXMLElement($str);
$result = $xml->getDocNamespaces(TRUE, TRUE);
var_dump($result);
?>
</body>
</head>
</html>
這將產生以下輸出 -
array(2) { ["t"]=> string(21) "http://example.org/ns" ["test"]=> string(20) "http://demo.com/test" }
相關用法
- PHP SimpleXMLElement::getName()用法及代碼示例
- PHP SimpleXMLElement::getNameSpaces()用法及代碼示例
- PHP SimpleXMLElement::xpath()用法及代碼示例
- PHP SimpleXMLElement::registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement::count()用法及代碼示例
- PHP SimpleXMLElement::__toString()用法及代碼示例
- PHP SimpleXMLElement::__construct()用法及代碼示例
- PHP SimpleXMLElement::addChild()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
- PHP SimpleXMLElement XPath()用法及代碼示例
- PHP SimpleXMLElement addAttribute()用法及代碼示例
- PHP SimpleXMLElement addChild()用法及代碼示例
- PHP SimpleXMLElement attributes()用法及代碼示例
- PHP SimpleXMLElement __toString()用法及代碼示例
- PHP SimpleXMLElement asXML()用法及代碼示例
- PHP SimpleXMLElement count()用法及代碼示例
- PHP SimpleXMLElement registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement getNamespaces()用法及代碼示例
- PHP SimpleXMLElement getDocNamespaces()用法及代碼示例
- PHP SimpleXMLElement saveXML()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP - SimpleXMLElement::getDocNamespaces() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。