Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | XMLReader moveToFirstAttribute() Function

PHP | XMLReader moveToFirstAttribute() Function

The XMLReader::moveToFirstAttribute() function is an inbuilt function in PHP which is used to position cursor on the first attribute of the element. This function is useful when we have multiple attributes for an element and we want to get the first one or when we want to check if an element has any attributes or not.

Syntax:

bool XMLReader::moveToFirstAttribute( void )

Parameters: This function doesn’t accept any parameter.

Return Value: This function returns TRUE on success or FALSE on failure.

Below examples illustrate the XMLReader::moveToFirstAttribute() function in PHP:

Example 1:

  • data.xml




    <?xml version="1.0" encoding="utf-8"?>
    <div>
        <h1>Foo Bar</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 node
    $XMLReader->read();
    $XMLReader->read();
    $XMLReader->read();
      
    // Checking if attribute is there or not
    if ($XMLReader->moveToFirstAttribute()) {
        echo "Attribute is there";
    } else {
        echo "No, attributes.";
    }
    ?>

    
    
  • Output:
    No, attributes.

Program 2:

  • data.xml




    <?xml version="1.0" encoding="utf-8"?>
    <div>
        <h1 attrib1="value1" 
            attrib2="value2" 
            attrib3="value"
         Foo Bar 
        </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 node
    $XMLReader->read();
    $XMLReader->read();
    $XMLReader->read();
      
    // Move to attribute with name attrib3
    $XMLReader->moveToAttribute("attrib3");
      
    // Print name of element
    echo "Before:<br> We are currently "
          . "at: $XMLReader->name<br>";
      
    // Move to first attribute
    $XMLReader->moveToFirstAttribute();
      
    // Print name of element
    echo "After:<br> We are currently "
          . "at: $XMLReader->name";
    ?>

    
    
  • Output:
    Before:
    We are currently at: attrib3
    After:
    We are currently at: attrib1

Reference: https://www.php.net/manual/en/xmlreader.movetofirstattribute.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments