Thursday, April 21

DOM to Parse XML and HTML

As you have seen we can easily use DOM to manipulate XML and HTML and it's really very very simple.

You use the $node->getElementsByTagName('title')->item(0)->nodeValue , for instance the <title> tag, you can grab the title.

If you have many nodes, you do a for each loop. Say you have 3 list items in <UL>, you can declare $listitems = $doc->getElementsByTagName('li').
Now we have a group of list items, all we have to do now is loop through them and get our stuff. for each ($listitems as $listitem){ $listitem_output = $listitem->item(0)->nodeValue; echo $listitem_output;}
Isn't that great?

Feel free to checkout some posts on how to get attributes using the getAttribute property from DOM, very exciting stuff this is.
 

Template by James William at 2600 Degrees