PHP SimpleXMLElement attribute () function

PHP

Function SimpleXMLElement::attribute() - it is a built-in function in PHP that is used to extract attributes and their values ​​from an XML tag in a SimpleXML object.Syntax:
 SimpleXMLElement SimpleXMLElement::attributes ($namespace, $is_prefix)
Parameter:This function takes two parameters as above and described below:
  • $namespace:This is an optional parameter. It defines the namespace for the extracted attribute.
  • $is_prefix:is a boolean parameter. This is true if $namespace is a prefix, and False if $namespace is a URI. The default is False.
Returned value:Returns a SimpleXMLElement object that can be iterated over by the attributes of the SimpleXMLElement object tag. It returns zero if the SimpleXMLElement object is already an attribute and not a tag.Note.This function is available in PHP 5.0.1 and later.The following programs illustrate the SimpleXMLElement::attribute() function in PHP:Program 1:  
// Load XML document into $user $user = < < < XML < user > < username > Geeks123 < / username > < name > GeeksforGeeks < / name > < phone > + 91-XXXXXXXXXX < / phone > < address font-color = " blue " font = "awsome-fonts" font-size = " 24px " > Noida, UP, India < / address > < / user >
XML;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Print the children attribute with its value foreach ( $xml -> address [0] -> attributes() as $key = > $value ) { echo $key . "= >" . $value . "< / br >" ; }  
?>
Output:
font-color = > blue font = > awsome-fonts font-size = > 24px
Program 2:  
// Load XML document into $user $user = < < < XML < user > < username font-color = "green" font = "awsome-fonts" font-size = "72px" > Geeks123 < / username > < name font-color = " blue " font = "awsome-fonts" font-size = " 36px " > GeeksforGeeks < / name > < phone font-color = " blue " type = "number" font = " awsome-fonts " font-size = " 24px " > + 91-XXXXXXXXXX < / phone > < address font-color = " blue " font = "awsome-fonts" font-size = " 24px " > Noida, UP, India < / address > < / user >
XML;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Print children attribute< p> foreach ( $xml -> children() as $child ) { echo "Child name:" . $child -> getName() . "= >" . $child . "< br >" ; foreach ( $child -> attributes() as $key = > $value ) echo " parameter:" . $key . "= >" . $value . "< / br >" ; }  
?>
Output:
Child name: username = > Geeks123 parameter: font-color = > green parameter: font = > awsome-fonts parameter: font-size = > 72px Child name: name = > GeeksforGeeks parameter: font-color = > blue parameter: font = > awsome-fonts parameter: font-size = > 36px Child name: phone = > + 91-XXXXXXXXXX parameter: font-color = > blue parameter: type = > number parameter: font = > awsome-fonts parameter: font-size = > 24px Child name: address = > Noida, UP, India parameter: font-color = > blue parameter: font = > awsome-fonts parameter: font-size = > 24px
Link: https://www.php.net /manual/en/simplexmlelement.attributes.php



Get Solution for free from DataCamp guru