How to use addAttribute in PHP

In this article I will explain how addAttribute function used in PHP.
  • 1698

addAttribute() Function

This function add an attribute to xml element.

Syntax

class SimpleXMLElement
{
string addAttribute(name,value,ns)
}


Example

XML Code

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tanya says</to>
<from> to Sachin</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note

PHP Code

<html>
<body>
<?php
$xml = simplexml_load_file("D:\sachin\parse.xml");
$xml->body[0]->addAttribute("Rock", "Star");
foreach($xml->body[0]->attributes() as $a => $b)
{
echo $a,'="',$b,"\"</br>";
}
?>
</html>
</body>

Output

addAttribute-php.jpg

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.