定義和用法
XML 是一種 mark-up 語言,用於在網絡上共享數據,XML 用於人類 read-able 和機器 read-able。 XMLReader 擴展用於讀取/檢索 XML 文檔的內容,即使用 XMLReader 類的方法可以讀取 XML 文檔的每個節點。
這個XMLReader::expand()XMLReader 類的函數複製當前節點的內容並將其作為 DOM 對象返回。
用法
XMLReader::expand([$base]);
參數
Sr.No | 參數及說明 |
---|---|
1 |
base(Mandatory) 這是代表目標文檔的 DOMNode 類的對象。 |
返回值
這個函數返回一個 DOMNode 類的對象。如果有問題,它返回布爾值 FALSE。
PHP版本
這個函數最初是在 PHP 版本 5 中引入的,並且適用於所有後續版本。
示例
以下是帶有可選參數的此函數的示例 -
data.xml
<Tutorials>
<Tutorial>
<Name>JavaFX</Name>
<Pages>535</Pages>
<Author>Krishna</Author>
<Version>11</Version>
</Tutorial>
<Tutorial>
<Name>CoffeeScript</Name>
<Pages>235</Pages>
<Author>Kasyap</Author>
<Version>2.5.1</Version>
</Tutorial>
</Tutorials>
sample.php
<?php
//Creating an XMLReader
$reader = new XMLReader();
//Opening a reader
$reader->open("mydata.xml");
//Reading the contents
$reader->read();
$data = $reader->expand();
print_r($data);
//Closing the reader
$reader->close();
?>
這將產生以下結果 -
DOMElement Object ( [tagName] => Tutorials [schemaTypeInfo] => [nodeName] => Tutorials [nodeValue] => JavaFX 535 Krishna 11 CoffeeScript 235 Kasyap 2.5.1 [nodeType] => 1 [parentNode] => [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => [nextSibling] => [attributes] => (object value omitted) [namespaceURI] => [prefix] => [localName] => Tutorials [baseURI] => [textContent] => JavaFX 535 Krishna 11 CoffeeScript 235 Kasyap 2.5.1 )
示例
以下是此函數的另一個示例 -
<?php
//Creating an XMLReader
$reader = new XMLReader();
$data = "<data>
<name>Raju</name>
<age>32</age>
<phone>9848022338</phone>
<city>Hyderabad</city>
</data> ";
//Opening a reader
$reader->xml($data);
//Reading the contents
$reader->read();
$data = $reader->expand();
print_r($data);
//Closing the reader
$reader->close();
?>
這將產生以下結果 -
DOMElement Object ( [tagName] => data [schemaTypeInfo] => [nodeName] => data [nodeValue] => Raju 32 9848022338 Hyderabad [nodeType] => 1 [parentNode] => [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => [nextSibling] => [attributes] => (object value omitted) [namespaceURI] => [prefix] => [localName] => data [baseURI] => [textContent] => Raju 32 9848022338 Hyderabad )
示例
mydata.xml
<data>
<name>Raju</name>
<age>32</age>
<phone>9848022338</phone>
<city>Hyderabad</city>
</data>
sample.php
<?php
//Creating an XMLReader
$reader = new XMLReader();
//Opening a reader
$reader->open("test.xml");
//Reading the contents
$reader->read();
$ele = $reader->expand();
$doc = new DOMDocument();
$doc->appendChild($ele);
$res = $doc->saveXML();
print_r($res);
//Closing the reader
$reader->close();
?>
這將產生以下結果 -
<?xml version="1.0"?> <data> <name>Raju</name> <age>32</age> <phone>9848022338</phone> <city>Hyderabad</city> </data>
相關用法
- PHP XMLReader::getParserProperty()用法及代碼示例
- PHP XMLReader::open()用法及代碼示例
- PHP XMLReader::moveToAttributeNs()用法及代碼示例
- PHP XMLReader::moveToNextAttribute()用法及代碼示例
- PHP XMLReader::moveToAttribute()用法及代碼示例
- PHP XMLReader::next()用法及代碼示例
- PHP XMLReader::setParserProperty()用法及代碼示例
- PHP XMLReader::lookupNamespace()用法及代碼示例
- PHP XMLReader::moveToElement()用法及代碼示例
- PHP XMLReader::moveToAttributeNo()用法及代碼示例
- PHP XMLReader::getAttributeNs()用法及代碼示例
- PHP XMLReader::XML()用法及代碼示例
- PHP XMLReader::isValid()用法及代碼示例
- PHP XMLReader::read()用法及代碼示例
- PHP XMLReader::readOuterXml()用法及代碼示例
- PHP XMLReader::moveToFirstAttribute()用法及代碼示例
- PHP XMLReader readOuterXml()用法及代碼示例
- PHP XMLReader setRelaxNGSchema()用法及代碼示例
- PHP XMLReader getAttributeNo()用法及代碼示例
- PHP XMLReader getParserProperty()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP - XMLReader::expand() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。