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


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

XMLReader::readString()函數是PHP中的內置函數,用於將當前節點的內容作為字符串讀取。 readString()與getInnerXml()的區別在於,前者不包括標記,而僅包含子節點的內容。

用法:

string XMLReader::readString( void )

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



返回值:該函數在失敗時以字符串或空字符串的形式返回當前節點的內容。

以下示例說明了PHP中的XMLReader::readString()函數:

範例1:在此程序中,我們將讀取不包含sub-nodes的元素的值。

  • data.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <div> 
        <h1>GeeksforGeeks</h1> 
    </div>
  • 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 
    $XMLReader->read(); 
    $XMLReader->read(); 
    $XMLReader->read(); 
      
    // Print the XML content 
    echo "The text inside is:" 
      . $XMLReader->readString(); 
    ?>
  • 輸出:
    The text inside is:GeeksforGeeks

例:在此程序中,我們將使用sub-nodes讀取元素的值。

  • data.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <div> 
        <h1>Hello World  
           <sub>G4G</sub> 
        </h1> 
    </div>
  • 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 
    $XMLReader->read(); 
    $XMLReader->read(); 
    $XMLReader->read(); 
      
    // Print the XML content 
    echo "The text inside is:" 
      . $XMLReader->readString(); 
    ?>
  • 輸出:
    The text inside is:Hello World G4G

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




相關用法


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