• PHP Video Tutorials

PHP SimpleXMLElement::addAttribute() Function



Definition and Usage

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The SimpleXMLElement class represents an XML document in PHP.

The SimpleXMLElement::addAttribute() function accepts string values representing type and value of an attribute and adds the specified attribute to the SimpleXML element.

Syntax

SimpleXMLElement::addAttribute($name [$value, $namespace ]);

Parameters

Sr.No Parameter & Description
1

name (Mandatory)

This is a string value representing the name of the attribute to be added to the SimpleXMLElement (XML file).

2

value(Optional)

This is a string representing the value of the attribute to be added to the SimpleXMLElement (XML file).

3

namespace(Optional)

This is a string value representing the namespace to which the attribute belongs.

Return Values

This function does not return any value.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Following example demonstrates the usage of the SimpleXMLIterator::addAttribute() function.

<html>
   <head>
      <body>
         <?php
            $xmlstr="<?xml version='1.0' standalone='yes'?>
            <Tutorial>
               <Name>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>";
            $xml = new SimpleXMLElement($xmlstr);
            $xml->addAttribute('type', 'test');
            $tut = $xml->addChild('Tutorial');
            $tut->addChild('Price', '600');
    
            echo $xml->asXML();
            echo "<br><br>";
            print_r($xml);
         ?>      
      </body>
   </head>   
</html>

This will produce following result −

JavaFX 535 Krishna 11 600
SimpleXMLElement Object (
   [@attributes] => Array ( [type] => test ) 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 11 
   [Tutorial] => SimpleXMLElement Object ( [Price] => 600 ) 
)

Example

Following is an example of this function with optional parameter namespace −

<html>
   <head>
      <body>
         <?php
            $str = "<?xml version='1.0' standalone='yes'?>
            <Tutorial>
               <Name>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>12</Version>
            </Tutorial>";
            $xml = new SimpleXMLElement($str);
            $xml->addAttribute("m:type", "test", 'mynamespace'); 
            print_r($xml);			 
         ?>      
      </body>
   </head>   
</html> 

This will produce the following output −

SimpleXMLElement Object ( 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 12 
)

Example

In the following example we are trying to load the contents of an XML file using the current() and next() functions and add an attribute to it −

Data.xml:

<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
   <Tutorial>
      <Name>JavaFX</Name>
      <Pages>535</Pages>
      <Author>Krishna</Author>
      <Version>11</Version>
   </Tutorial>

   <Tutorial>
      <Name>CoffeeScript</Name>
      <Pages>235</Pages>
      <Author>Kasyap</Author>
      <Version>2.5.1</Version>
   </Tutorial>
</Tutorials> 

Sample.xml

<html>
   <head>      
      <body>         
         <?php
            $doc = new DOMDocument;
            $xml = simplexml_load_file("Data.xml");
            
            //file to SimpleXMLElement 
            $simpleXmlElement = simplexml_import_dom($xml);
            $simpleXmlElement->addAttribute('type', 'test');
            print_r($xml);

            print("<br><br>");
            foreach($xml->children() as $tut) {       
               print($tut->Name ."<br>");
               print($tut->Pages ."<br>");
               print($tut->Author ."<br>");
               print($tut->Version ."<br>");
               print("<br>");
            }
         ?>
      </body>
   </head>
</html> 

This will produce following result −

SimpleXMLElement Object ( [@attributes] => 
Array ( [type] => test ) [Tutorial] => 
Array ( [0] => SimpleXMLElement Object ( [Name] => 
JavaFX [Pages] => 535 [Author] => 
Krishna [Version] => 11 ) [1] => 
SimpleXMLElement Object ( [Name] => 
CoffeeScript [Pages] => 235 [Author] => 
Kasyap [Version] => 2.5.1 ) ) )

JavaFX
535
Krishna
11

CoffeeScript
235
Kasyap
2.5.1
php_function_reference.htm
Advertisements