42

is there any easy way of getting all attributes of a node without checking if it has that attribute? short, here's an example of what i'm trying to do: i have this short domdocument:

<p align=center style="font-size: 12px;">some text</p>
<a href="#" target="_blank">some link<a/>

okay.. now if i check p tag with getAttribute('align') i'll get the center value.. that's cool, but i want to see if p tag has also another attribute like style without checking for every attribute possible. on img tag i'll have to check for src, width, height, style, onclick, etc.. to verify if they exists.. but i'm thinking it might be a easier way of seeing all attributes.

alin
  • 421
  • 1
  • 4
  • 3
  • 3
    *(sidenote)* A valid X(HT)ML document has to have a root element and all attributes have to be in quotes. – Gordon Mar 05 '10 at 10:10
  • *(hint)* If this is strictly for reading you might find http://de3.php.net/manual/en/book.simplexml.php or http://simplehtmldom.sourceforge.net/manual.htm easier to use. – Gordon Mar 05 '10 at 10:34

2 Answers2

78

Considering you have your node as a DOMElement or DOMNode, you can use the $attributes property of the DOMNode class : it contains a list of the attributes that the node has.

Using that property, you can loop over the attributes, getting the name and value of each one, with their $nodeName and $nodeValue properties.


For instance, in your case, you could use something like this :

$str = <<<STR
<p align=center style="font-size: 12px;">some text</p>
<a href="#" target="_blank">some link<a/>
STR;

$dom = new DOMDocument();
$dom->loadHTML($str);

$p = $dom->getElementsByTagName('p')->item(0);
if ($p->hasAttributes()) {
  foreach ($p->attributes as $attr) {
    $name = $attr->nodeName;
    $value = $attr->nodeValue;
    echo "Attribute '$name' :: '$value'<br />";
  }
}


Which would get you this kind of output :

Attribute 'align' :: 'center'
Attribute 'style' :: 'font-size: 12px;'

i.e. we have the two attributes of the node, without knowing their names before ; and for each attribute, we can obtain its name and its value.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
-6

Use this code, it will give you the specified attributes.

 

<html>
<script>
function test()
{
getvalue=document.getElementById("iid").getAttribute("align")
alert ( getvalue) ;
}
</script>

<body>
<p id=iid align="center" background="red" onclick="test();" >
This is for testing
php dom get all attributes of a node
</p>
</body>
</html>

Now when you click the conent of the p tag . It will show you align attirbute values.

Pavunkumar
  • 5,147
  • 14
  • 43
  • 69
  • 6
    Question is tagged PHP, not JavaScript, so the OP probably wants to do this server side. – Gordon Mar 05 '10 at 10:20
  • Doesn't this simply display the value of the align attribute when clicking on the element? – Roberto Aloi Mar 05 '10 at 10:21
  • @Gordon: Good spot. @alin: Could you clarify and (in case) adjust the tags? – Roberto Aloi Mar 05 '10 at 10:23
  • @pavun_cool: i wanted to get all attributes without having a list and guessing which one is present. and in php not js. the below code pascal provided works like a charm – alin Mar 05 '10 at 12:07