XMLReader::expand()函數是PHP中的內置函數,用於複製當前節點並返回適當的DOM對象。
用法:
DOMNode XMLReader::expand( DOMNode $basenode )
參數:該函數接受單個參數$basenode,該參數包含一個DOMNode,該DOMNode定義了所創建DOM對象的目標DOMDocument。
返回值:如果成功,則此函數返回DOMNode;如果失敗,則返回FALSE。
以下示例說明了PHP中的XMLReader::expand()函數:
範例1:
- data.xml
<?xml version="1.0" encoding="utf-8"?> <root> <div> This is a div </div> </root>
- index.php
<?php // Create a new XMLReader instance $XMLReader = new XMLReader(); // Open the XML file $XMLReader->open('data.xml'); // Move to the first node $XMLReader->read(); // Read it as a element $element = $XMLReader->expand(); // Print the node value to the browser echo $element->nodeValue; ?>
- 輸出:
This is a div
範例2:
- data.xml
<?xml version="1.0" encoding="utf-8"?> <body> <h1 style="color:green; font-size:100px;"> GeeksforGeeks </h1> </body>
- index.php
<?php // Create a new XMLReader instance $XMLReader = new XMLReader(); // Open the XML file $XMLReader->open('data.xml'); // Move to the first node $XMLReader->read(); // Read it as a element $element = $XMLReader->expand(); // Create a new DOMDocument instance $DOMDocument = new DOMDocument(); // Append the child to the element $DOMDocument->appendChild($element); // Render the XML in browser echo $DOMDocument->saveXML();
- 輸出:
參考: https://www.php.net/manual/en/xmlreader.expand.php
相關用法
- PHP XMLReader XML()用法及代碼示例
- PHP XMLReader setRelaxNGSchemaSource()用法及代碼示例
- PHP XMLReader setSchema()用法及代碼示例
- PHP XMLReader getAttributeNs()用法及代碼示例
- PHP XMLReader getAttribute()用法及代碼示例
- PHP XMLReader close()用法及代碼示例
- PHP XMLReader open()用法及代碼示例
- PHP XMLReader getAttributeNo()用法及代碼示例
- PHP XMLReader readInnerXml()用法及代碼示例
- PHP XMLReader getParserProperty()用法及代碼示例
- PHP XMLReader setRelaxNGSchema()用法及代碼示例
- PHP XMLReader setParserProperty()用法及代碼示例
- PHP XMLReader readString()用法及代碼示例
- PHP XMLReader readOuterXml()用法及代碼示例
- PHP XMLReader moveToNextAttribute()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader expand() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。