Remove DOM Node from XML


/ Published in: PHP
Save to your folder(s)

Manipulates XML nodes using PHP's Dom library. Includes XPath navigation, child manipulation, and deletion of current document.


Copy this code and paste it in your HTML
  1. function formatIFRAMECells1($oDom)
  2. {
  3. $oXpath = new DOMXpath($oDom);
  4. $oElements = $oXpath->query("//td[@class='tditem']");
  5.  
  6. //Remove the TD element to the right of the logo cell. This xpath query finds the following TD element:
  7. // <td bgcolor="#FFFFFF" height="2" class="tditem">
  8. //
  9. // Only remove it if it's also followed by:
  10. //<div align="left"><img src="http://www.mobiles4everyone.com/images/horizontallineplain.jpg" width="100%" height="1"></div>
  11.  
  12. if (!is_null($oElements))
  13. {
  14. foreach ($oElements as $oElement)
  15. {
  16. if($oElement->hasChildNodes())
  17. {
  18. $oChildren = $oElement->childNodes;
  19. for($i=0;$i<$oChildren->length;$i++)
  20. {
  21. $oChild = $oChildren->item($i);
  22. if($oChild->nodeName == "div")
  23. {
  24.  
  25. if($oChild->hasChildNodes())
  26. {
  27. $oDivChild = $oChild->firstChild;
  28. if($oDivChild->nodeName == "img")
  29. {
  30. $oElement->parentNode->removeChild($oElement);
  31. break;
  32. }
  33. }
  34. }
  35.  
  36. }
  37. }
  38. }
  39. }
  40. return $oDom;
  41. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.