當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP XMLReader read()用法及代碼示例

XMLReader::read()函數是PHP中的內置函數,用於移至文檔中的下一個節點。因此,此函數用於遍曆XML文檔。

用法:

bool XMLReader::read( void )

參數:此函數不接受任何參數。



返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。

下麵給出的程序說明了PHP中的XMLReader::read()函數:

程序1:在此程序中,遍曆文件後將獲得元素的值data.xml

文件名:data.xml

<?xml version="1.0" encoding="utf-8"?> 
<div1> 
    <h1> GeeksforGeeks </h1> 
</div1>

文檔名稱: index.php

<?php 
  
// Create a new XMLReader instance 
$XMLReader = new XMLReader(); 
  
// Open the XML file 
$XMLReader->open('data.xml'); 
  
// Iterate through the XML nodes to 
// reach the h1 element's text  
// (Only four times) 
$XMLReader->read(); 
$XMLReader->read(); 
$XMLReader->read(); 
$XMLReader->read(); 
  
// Print the value of element 
echo "The text inside is:"
    . "$XMLReader->value<br>"; 
?>

輸出:

GeeksforGeeks

程序2:在此程序中,遍曆元素後將獲得它的名稱。

文檔名稱: data.xml

<?xml version="1.0" encoding="utf-8"?> 
<div1> 
    <h1> GeeksforGeeks </h1> 
</div1>

文檔名稱: index.php

<?php 
  
// Create a new XMLReader instance 
$XMLReader = new XMLReader(); 
  
// Open the XML file 
$XMLReader->open('data.xml'); 
  
// Iterate through the XML nodes 
// to reach the h1 element 
// (only three times) 
$XMLReader->read(); 
$XMLReader->read(); 
$XMLReader->read(); 
  
// Print name of element 
echo "The name of element is:"
     . "$XMLReader->name<br>"; 
?>

輸出:

The name of element is:h1

參考: https://www.php.net/manual/en/xmlreader.read.php




相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader read() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。