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.

3 comments:

  1. In our web design company, we make it so easy to manipulate XML and HTML as it was really interesting and easy to make it.Ur blog made much more curious to know about it.

    ReplyDelete
  2. Wonderful post. I do hope you intend to post more of these types of information. Thank you for this interesting information..
    Web Development Company

    ReplyDelete
  3. Thanks for that RSS post. I wish you would have listed the code portion a little more clearly. Not sure of the code "foreach ($listitems as $listitem){
    $listitem_output = $listitem->item(0)->nodeValue;
    echo $listitem_output;
    };

    I can't get that to go. Are you still following this thread? Thanks!

    ReplyDelete

 

Template by James William at 2600 Degrees