PHP - XmlDom php4tophp5


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



Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3.  Requires PHP5, uses built-in DOM extension.
  4.  To be used in PHP4 scripts using DOMXML extension.
  5.  Allows PHP4/DOMXML scripts to run on PHP5/DOM.
  6.  (Requires PHP5/XSL extension for domxml_xslt functions)
  7.  
  8.  Typical use:
  9.  {
  10.   if (version_compare(PHP_VERSION,'5','>='))
  11.   require_once('domxml-php4-to-php5.php');
  12.  }
  13.  
  14.  Version 1.12, 2006-10-11, http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
  15.  
  16.  ------------------------------------------------------------------
  17.  Written by Alexandre Alapetite, http://alexandre.alapetite.net/cv/
  18.  
  19.  Copyright 2004-2006, Licence: Creative Commons "Attribution-ShareAlike 2.0 France" BY-SA (FR),
  20.  http://creativecommons.org/licenses/by-sa/2.0/fr/
  21.  http://alexandre.alapetite.net/divers/apropos/#by-sa
  22.  - Attribution. You must give the original author credit
  23.  - Share Alike. If you alter, transform, or build upon this work,
  24.   you may distribute the resulting work only under a license identical to this one
  25.   (Can be included in GPL/LGPL projects)
  26.  - The French law is authoritative
  27.  - Any of these conditions can be waived if you get permission from Alexandre Alapetite
  28.  - Please send to Alexandre Alapetite the modifications you make,
  29.   in order to improve this file for the benefit of everybody
  30.  
  31.  If you want to distribute this code, please do it as a link to:
  32.  http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
  33. */
  34.  
  35. function domxml_new_doc($version) {return new php4DOMDocument('');}
  36. function domxml_new_xmldoc($version) {return new php4DOMDocument('');}
  37. function domxml_open_file($filename) {return new php4DOMDocument($filename);}
  38. function domxml_open_mem($str)
  39. {
  40. $dom=new php4DOMDocument('');
  41. $dom->myDOMNode->loadXML($str);
  42. return $dom;
  43. }
  44. function html_doc($html_doc,$from_file=false)
  45. {
  46. $dom=new php4DOMDocument('');
  47. if ($from_file) $dom->myDOMNode->loadHTMLFile($html_doc);
  48. else $dom->myDOMNode->loadHTML($html_doc);
  49. return $dom;
  50. }
  51. function html_doc_file($filename) {return html_doc($filename,true);}
  52. function xmldoc($str) {return domxml_open_mem($str);}
  53. function xmldocfile($filename) {return new php4DOMDocument($filename);}
  54. function xpath_eval($xpath_context,$eval_str,$contextnode=null) {return $xpath_context->xpath_eval($eval_str,$contextnode);}
  55. function xpath_new_context($dom_document) {return new php4DOMXPath($dom_document);}
  56. function xpath_register_ns($xpath_context,$prefix,$namespaceURI) {return $xpath_context->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
  57.  
  58. class php4DOMAttr extends php4DOMNode
  59. {
  60. function name() {return $this->myDOMNode->name;}
  61. function set_value($content) {return $this->myDOMNode->value=$content;}
  62. function specified() {return $this->myDOMNode->specified;}
  63. function value() {return $this->myDOMNode->value;}
  64. }
  65.  
  66. class php4DOMDocument extends php4DOMNode
  67. {
  68. function php4DOMDocument($filename='')
  69. {
  70. $this->myDOMNode=new DOMDocument();
  71. $this->myOwnerDocument=$this;
  72. if ($filename!='') $this->myDOMNode->load($filename);
  73. }
  74. function add_root($name)
  75. {
  76. if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
  77. return new php4DOMElement($this->myDOMNode->appendChild($this->myDOMNode->createElement($name)),$this->myOwnerDocument);
  78. }
  79. function create_attribute($name,$value)
  80. {
  81. $myAttr=$this->myDOMNode->createAttribute($name);
  82. $myAttr->value=$value;
  83. return new php4DOMAttr($myAttr,$this);
  84. }
  85. function create_cdata_section($content) {return new php4DOMNode($this->myDOMNode->createCDATASection($content),$this);}
  86. function create_comment($data) {return new php4DOMNode($this->myDOMNode->createComment($data),$this);}
  87. function create_element($name) {return new php4DOMElement($this->myDOMNode->createElement($name),$this);}
  88. function create_element_ns($uri,$name,$prefix=null)
  89. {
  90. if ($prefix==null) $prefix=$this->myDOMNode->lookupPrefix($uri);
  91. if ($prefix==null) $prefix='a'.sprintf('%u',crc32($uri));
  92. return new php4DOMElement($this->myDOMNode->createElementNS($uri,$prefix.':'.$name),$this);
  93. }
  94. function create_text_node($content) {return new php4DOMText($this->myDOMNode->createTextNode($content),$this);}
  95. function document_element() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);}
  96. function dump_file($filename,$compressionmode=false,$format=false)
  97. {
  98. $format0=$this->myDOMNode->formatOutput;
  99. $this->myDOMNode->formatOutput=$format;
  100. $res=$this->myDOMNode->save($filename);
  101. $this->myDOMNode->formatOutput=$format0;
  102. return $res;
  103. }
  104. function dump_mem($format=false,$encoding=false)
  105. {
  106. $format0=$this->myDOMNode->formatOutput;
  107. $this->myDOMNode->formatOutput=$format;
  108. $encoding0=$this->myDOMNode->encoding;
  109. if ($encoding) $this->myDOMNode->encoding=$encoding;
  110. $dump=$this->myDOMNode->saveXML();
  111. $this->myDOMNode->formatOutput=$format0;
  112. if ($encoding) $this->myDOMNode->encoding= $encoding0=='' ? 'UTF-8' : $encoding0; //UTF-8 is XML default encoding
  113. return $dump;
  114. }
  115. function free()
  116. {
  117. if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild);
  118. $this->myDOMNode=null;
  119. $this->myOwnerDocument=null;
  120. }
  121. function get_element_by_id($id) {return parent::_newDOMElement($this->myDOMNode->getElementById($id),$this);}
  122. function get_elements_by_tagname($name)
  123. {
  124. $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
  125. $nodeSet=array();
  126. $i=0;
  127. if (isset($myDOMNodeList))
  128. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this);
  129. return $nodeSet;
  130. }
  131. function html_dump_mem() {return $this->myDOMNode->saveHTML();}
  132. function root() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);}
  133. function xpath_new_context() {return new php4DOMXPath($this);}
  134. }
  135.  
  136. class php4DOMElement extends php4DOMNode
  137. {
  138. function add_namespace($uri,$prefix)
  139. {
  140. if ($this->myDOMNode->hasAttributeNS('http://www.w3.org/2000/xmlns/',$prefix)) return false;
  141. else
  142. {
  143. $this->myDOMNode->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$prefix,$uri); //By Daniel Walker on 2006-09-08
  144. return true;
  145. }
  146. }
  147. function get_attribute($name) {return $this->myDOMNode->getAttribute($name);}
  148. function get_attribute_node($name) {return parent::_newDOMElement($this->myDOMNode->getAttributeNode($name),$this->myOwnerDocument);}
  149. function get_elements_by_tagname($name)
  150. {
  151. $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
  152. $nodeSet=array();
  153. $i=0;
  154. if (isset($myDOMNodeList))
  155. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument);
  156. return $nodeSet;
  157. }
  158. function has_attribute($name) {return $this->myDOMNode->hasAttribute($name);}
  159. function remove_attribute($name) {return $this->myDOMNode->removeAttribute($name);}
  160. function set_attribute($name,$value)
  161. {
  162. //return $this->myDOMNode->setAttribute($name,$value); //Does not return a DomAttr
  163. $myAttr=$this->myDOMNode->ownerDocument->createAttribute($name);
  164. $myAttr->value=$value;
  165. $this->myDOMNode->setAttributeNode($myAttr);
  166. return new php4DOMAttr($myAttr,$this->myOwnerDocument);
  167. }
  168. function set_attribute_node($attr)
  169. {
  170. $this->myDOMNode->setAttributeNode($this->_importNode($attr));
  171. return $attr;
  172. }
  173. function set_name($name)
  174. {
  175. if ($this->myDOMNode->prefix=='') $newNode=$this->myDOMNode->ownerDocument->createElement($name);
  176. else $newNode=$this->myDOMNode->ownerDocument->createElementNS($this->myDOMNode->namespaceURI,$this->myDOMNode->prefix.':'.$name);
  177. $myDOMNodeList=$this->myDOMNode->attributes;
  178. $i=0;
  179. if (isset($myDOMNodeList))
  180. while ($node=$myDOMNodeList->item($i++))
  181. if ($node->namespaceURI=='') $newNode->setAttribute($node->name,$node->value);
  182. else $newNode->setAttributeNS($node->namespaceURI,$node->nodeName,$node->value);
  183. $myDOMNodeList=$this->myDOMNode->childNodes;
  184. if (isset($myDOMNodeList))
  185. while ($node=$myDOMNodeList->item(0)) $newNode->appendChild($node);
  186. $this->myDOMNode->parentNode->replaceChild($newNode,$this->myDOMNode);
  187. $this->myDOMNode=$newNode;
  188. return true;
  189. }
  190. function tagname() {return $this->myDOMNode->tagName;}
  191. }
  192.  
  193. class php4DOMNode
  194. {
  195. public $myDOMNode;
  196. public $myOwnerDocument;
  197. function php4DOMNode($aDomNode,$aOwnerDocument)
  198. {
  199. $this->myDOMNode=$aDomNode;
  200. $this->myOwnerDocument=$aOwnerDocument;
  201. }
  202. function __get($name)
  203. {
  204. switch ($name)
  205. {
  206. case 'type': return $this->myDOMNode->nodeType;
  207. case 'tagname': return $this->myDOMNode->tagName;
  208. case 'content': return $this->myDOMNode->textContent;
  209. case 'name': return $this->myDOMNode->name;
  210. case 'value': return $this->myDOMNode->value;
  211. default:
  212. $myErrors=debug_backtrace();
  213. trigger_error('Undefined property: '.get_class($this).'::$'.$name.' ['.$myErrors[0]['file'].':'.$myErrors[0]['line'].']',E_USER_NOTICE);
  214. return false;
  215. }
  216. }
  217. function add_child($newnode) {return append_child($newnode);}
  218. function add_namespace($uri,$prefix) {return false;}
  219. function append_child($newnode) {return self::_newDOMElement($this->myDOMNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);}
  220. function append_sibling($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);}
  221. function attributes()
  222. {
  223. $myDOMNodeList=$this->myDOMNode->attributes;
  224. $nodeSet=array();
  225. $i=0;
  226. if (isset($myDOMNodeList))
  227. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMAttr($node,$this->myOwnerDocument);
  228. return $nodeSet;
  229. }
  230. function child_nodes()
  231. {
  232. $myDOMNodeList=$this->myDOMNode->childNodes;
  233. $nodeSet=array();
  234. $i=0;
  235. if (isset($myDOMNodeList))
  236. while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=self::_newDOMElement($node,$this->myOwnerDocument);
  237. return $nodeSet;
  238. }
  239. function children() {return $this->child_nodes();}
  240. function clone_node($deep=false) {return self::_newDOMElement($this->myDOMNode->cloneNode($deep),$this->myOwnerDocument);}
  241. //dump_node($node) should only be called on php4DOMDocument
  242. function dump_node($node=null) {return $node==null ? $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode) : $this->myOwnerDocument->myDOMNode->saveXML($node->myDOMNode);}
  243. function first_child() {return self::_newDOMElement($this->myDOMNode->firstChild,$this->myOwnerDocument);}
  244. function get_content() {return $this->myDOMNode->textContent;}
  245. function has_attributes() {return $this->myDOMNode->hasAttributes();}
  246. function has_child_nodes() {return $this->myDOMNode->hasChildNodes();}
  247. function insert_before($newnode,$refnode) {return self::_newDOMElement($this->myDOMNode->insertBefore($this->_importNode($newnode),$refnode->myDOMNode),$this->myOwnerDocument);}
  248. function is_blank_node() {return ($this->myDOMNode->nodeType==XML_TEXT_NODE)&&preg_match('%^\s*$%',$this->myDOMNode->nodeValue);}
  249. function last_child() {return self::_newDOMElement($this->myDOMNode->lastChild,$this->myOwnerDocument);}
  250. function new_child($name,$content)
  251. {
  252. $mySubNode=$this->myDOMNode->ownerDocument->createElement($name);
  253. $mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(html_entity_decode($content,ENT_QUOTES)));
  254. $this->myDOMNode->appendChild($mySubNode);
  255. return new php4DOMElement($mySubNode,$this->myOwnerDocument);
  256. }
  257. function next_sibling() {return self::_newDOMElement($this->myDOMNode->nextSibling,$this->myOwnerDocument);}
  258. function node_name() {return ($this->myDOMNode->nodeType==XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->nodeName;} //Avoid namespace prefix for DOMElement
  259. function node_type() {return $this->myDOMNode->nodeType;}
  260. function node_value() {return $this->myDOMNode->nodeValue;}
  261. function owner_document() {return $this->myOwnerDocument;}
  262. function parent_node() {return self::_newDOMElement($this->myDOMNode->parentNode,$this->myOwnerDocument);}
  263. function prefix() {return $this->myDOMNode->prefix;}
  264. function previous_sibling() {return self::_newDOMElement($this->myDOMNode->previousSibling,$this->myOwnerDocument);}
  265. function remove_child($oldchild) {return self::_newDOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode),$this->myOwnerDocument);}
  266. function replace_child($oldnode,$newnode) {return self::_newDOMElement($this->myDOMNode->replaceChild($oldnode->myDOMNode,$this->_importNode($newnode)),$this->myOwnerDocument);}
  267. function set_content($text) {return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode($text));}
  268. //function set_name($name) {return $this->myOwnerDocument->renameNode($this->myDOMNode,$this->myDOMNode->namespaceURI,$name);}
  269. function set_namespace($uri,$prefix=null)
  270. {//Contributions by Daniel Walker on 2006-09-08
  271. $nsprefix=$this->myDOMNode->lookupPrefix($uri);
  272. if ($nsprefix==null)
  273. {
  274. $nsprefix= $prefix==null ? $nsprefix='a'.sprintf('%u',crc32($uri)) : $prefix;
  275. if ($this->myDOMNode->nodeType==XML_ATTRIBUTE_NODE)
  276. {
  277. if (($prefix!=null)&&$this->myDOMNode->ownerElement->hasAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)&&
  278. ($this->myDOMNode->ownerElement->getAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)!=$uri))
  279. {//Remove namespace
  280. $parent=$this->myDOMNode->ownerElement;
  281. $parent->removeAttributeNode($this->myDOMNode);
  282. $parent->setAttribute($this->myDOMNode->localName,$this->myDOMNode->nodeValue);
  283. $this->myDOMNode=$parent->getAttributeNode($this->myDOMNode->localName);
  284. return;
  285. }
  286. $this->myDOMNode->ownerElement->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$nsprefix,$uri);
  287. }
  288. }
  289. if ($this->myDOMNode->nodeType==XML_ATTRIBUTE_NODE)
  290. {
  291. $parent=$this->myDOMNode->ownerElement;
  292. $parent->removeAttributeNode($this->myDOMNode);
  293. $parent->setAttributeNS($uri,$nsprefix.':'.$this->myDOMNode->localName,$this->myDOMNode->nodeValue);
  294. $this->myDOMNode=$parent->getAttributeNodeNS($uri,$this->myDOMNode->localName);
  295. }
  296. elseif ($this->myDOMNode->nodeType==XML_ELEMENT_NODE)
  297. {
  298. $NewNode=$this->myDOMNode->ownerDocument->createElementNS($uri,$nsprefix.':'.$this->myDOMNode->localName);
  299. foreach ($this->myDOMNode->attributes as $n) $NewNode->appendChild($n->cloneNode(true));
  300. foreach ($this->myDOMNode->childNodes as $n) $NewNode->appendChild($n->cloneNode(true));
  301. $xpath=new DOMXPath($this->myDOMNode->ownerDocument);
  302. $myDOMNodeList=$xpath->query('namespace::*[name()!="xml"]',$this->myDOMNode); //Add old namespaces
  303. foreach ($myDOMNodeList as $n) $NewNode->setAttributeNS('http://www.w3.org/2000/xmlns/',$n->nodeName,$n->nodeValue);
  304. $this->myDOMNode->parentNode->replaceChild($NewNode,$this->myDOMNode);
  305. $this->myDOMNode=$NewNode;
  306. }
  307. }
  308. protected function _importNode($newnode) {return $this->myOwnerDocument===$newnode->myOwnerDocument ? $newnode->myDOMNode : $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode,true);} //To import DOMNode from another DOMDocument
  309. static function _newDOMElement($aDOMNode,$aOwnerDocument)
  310. {//Check the PHP5 DOMNode before creating a new associated PHP4 DOMNode wrapper
  311. if ($aDOMNode==null) return null;
  312. switch ($aDOMNode->nodeType)
  313. {
  314. case XML_ELEMENT_NODE: return new php4DOMElement($aDOMNode,$aOwnerDocument);
  315. case XML_TEXT_NODE: return new php4DOMText($aDOMNode,$aOwnerDocument);
  316. case XML_ATTRIBUTE_NODE: return new php4DOMAttr($aDOMNode,$aOwnerDocument);
  317. default: return new php4DOMNode($aDOMNode,$aOwnerDocument);
  318. }
  319. }
  320. }
  321.  
  322. class php4DOMText extends php4DOMNode
  323. {
  324. function __get($name)
  325. {
  326. if ($name=='tagname') return '#text';
  327. else return parent::__get($name);
  328. }
  329. function tagname() {return '#text';}
  330. }
  331.  
  332. if (!defined('XPATH_NODESET'))
  333. {
  334. define('XPATH_UNDEFINED',0);
  335. define('XPATH_NODESET',1);
  336. /*define('XPATH_BOOLEAN',2);
  337.  define('XPATH_NUMBER',3);
  338.  define('XPATH_STRING',4);
  339.  define('XPATH_POINT',5);
  340.  define('XPATH_RANGE',6);
  341.  define('XPATH_LOCATIONSET',7);
  342.  define('XPATH_USERS',8);
  343.  define('XPATH_XSLT_TREE',9);*/
  344. }
  345.  
  346. class php4DOMNodelist
  347. {//TODO: To be updated for PHP/5.1 to allow XPath boolean expressions etc. DOMXPath->evaluate()
  348. private $myDOMNodelist;
  349. public $nodeset;
  350. public $type;
  351. function php4DOMNodelist($aDOMNodelist,$aOwnerDocument)
  352. {
  353. $this->myDOMNodelist=$aDOMNodelist;
  354. $this->nodeset=array();
  355. $i=0;
  356. if (isset($this->myDOMNodelist))
  357. {
  358. $this->type=XPATH_NODESET;
  359. while ($node=$this->myDOMNodelist->item($i++)) $this->nodeset[]=php4DOMNode::_newDOMElement($node,$aOwnerDocument);
  360. }
  361. else $this->type=XPATH_UNDEFINED;
  362. }
  363. }
  364.  
  365. class php4DOMXPath
  366. {
  367. public $myDOMXPath;
  368. private $myOwnerDocument;
  369. function php4DOMXPath($dom_document)
  370. {
  371. //TODO: If $dom_document is a DomElement, make that default $contextnode and modify XPath. Ex: '/test'
  372. $this->myOwnerDocument=$dom_document->myOwnerDocument;
  373. $this->myDOMXPath=new DOMXPath($this->myOwnerDocument->myDOMNode);
  374. }
  375. function xpath_eval($eval_str,$contextnode=null) {return isset($contextnode) ? new php4DOMNodelist($this->myDOMXPath->query($eval_str,$contextnode->myDOMNode),$this->myOwnerDocument) : new php4DOMNodelist($this->myDOMXPath->query($eval_str),$this->myOwnerDocument);}
  376. function xpath_register_ns($prefix,$namespaceURI) {return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);}
  377. }
  378.  
  379. if (extension_loaded('xsl'))
  380. {//See also: http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/
  381. function domxml_xslt_stylesheet($xslstring) {return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));}
  382. function domxml_xslt_stylesheet_doc($dom_document) {return new php4DomXsltStylesheet($dom_document);}
  383. function domxml_xslt_stylesheet_file($xslfile) {return new php4DomXsltStylesheet(DOMDocument::load($xslfile));}
  384. class php4DomXsltStylesheet
  385. {
  386. private $myxsltProcessor;
  387. function php4DomXsltStylesheet($dom_document)
  388. {
  389. $this->myxsltProcessor=new xsltProcessor();
  390. $this->myxsltProcessor->importStyleSheet($dom_document);
  391. }
  392. function process($dom_document,$xslt_parameters=array(),$param_is_xpath=false)
  393. {
  394. foreach ($xslt_parameters as $param=>$value) $this->myxsltProcessor->setParameter('',$param,$value);
  395. $myphp4DOMDocument=new php4DOMDocument();
  396. $myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode);
  397. return $myphp4DOMDocument;
  398. }
  399. function result_dump_file($dom_document,$filename)
  400. {
  401. $html=$dom_document->myDOMNode->saveHTML();
  402. file_put_contents($filename,$html);
  403. return $html;
  404. }
  405. function result_dump_mem($dom_document) {return $dom_document->myDOMNode->saveHTML();}
  406. }
  407. }
  408. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.