How to use attributes in PHP

In this article I will explain how attributes function used in PHP.
  • 1261

attributes() Function

This function gets an XML elements attributes.

Syntax

class SimpleXMLElement
{
string attributes(ns,is_prefix)
}

 

 Parameter  Description
 ns  Optional
 is_prefix  Optional

Example

XML file

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


PHP Code

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

Output

attributes-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

© 2019 DotNetHeaven. All rights reserved.