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
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <div>
        <h1>Foo Bar</h1>
    </div>

    chevron_right

    
    

  • index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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.";
    }
    ?>

    chevron_right

    
    

  • Output:
    No, attributes.

Program 2:

  • data.xml
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <div>
        <h1 attrib1="value1" 
            attrib2="value2" 
            attrib3="value"
         Foo Bar 
        </h1>
    </div>

    chevron_right

    
    

  • index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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";
    ?>

    chevron_right

    
    

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

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.