[ Index ] |
PHP Cross Reference of phool |
[Summary view] [Print] [Text view]
1 <?php 2 //============================================================================ 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Lesser General Public License (LGPL) as 5 // published by the Free Software Foundation, either version 3 of the License, 6 // or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Lesser General Public License for more details. 12 // 13 // You should have received a copy of the GNU Lesser General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 //============================================================================ 16 /** 17 * @copyright Francois Laupretre <phool@tekwire.net> 18 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0 19 * @category phool 20 * @package phool 21 */ 22 //============================================================================ 23 //---------------------------------------------------------------------------- 24 /** 25 * This class allows to browse an XML or XHTML document 26 */ 27 //---------------------------------------------------------------------------- 28 29 //error_reporting(E_ERROR | E_CORE_ERROR | E_USER_ERROR); 30 31 //---------------------------------------------------------------------------- 32 33 namespace Phool\XML; 34 35 class XPage 36 { 37 38 /** @var string Buffer containing the document 39 40 public $page; 41 42 /** @var DOMDocument The document as a DOMDocument instance */ 43 44 public $xdoc=null; 45 46 #-------- 47 /** Creates the DOM instance and imports the data 48 * 49 * @param string $data The content of the page/document 50 * @param boolean $html Is the format html (or xml) ? 51 * @returns void 52 * @throws DOMException 53 */ 54 55 public function __construct($data,$html=true) 56 { 57 $this->page=$data; 58 $this->xdoc=new \DOMDocument; 59 60 $method=($html ? 'loadHTML' : 'loadXML'); 61 if (!@$this->xdoc->$method($data)) 62 throw new \DOMException('Cannot load data'); 63 } 64 65 #-------- 66 /** 67 * Returns a list of nodes corresponding to an XPath 68 * 69 * @param string $xpath The xpath string 70 * @param DOMElement|null The search base(if null, starts from the root node) 71 * 72 * @returns DOMNodeList (can be empty) 73 */ 74 75 public function nodes($xpath,$base=null) 76 { 77 $xp=new \DOMXPath($this->xdoc); 78 if (is_null($base)) $base=$this->xdoc->documentElement; 79 $n=$xp->query($xpath,$base); 80 unset($xp); 81 return $n; 82 } 83 84 //------------ 85 /** 86 * Returns the first node corresponding to an XPath 87 * 88 * @param string $xpath The xpath string 89 * @param DOMElement|null The search base(if null, starts from the root node) 90 * 91 * @returns DOMNode|null null if no match 92 */ 93 94 public function node($xpath,$base=null) 95 { 96 $node_list=$this->nodes($xpath,$base); 97 return ($node_list->length ? $node_list->item(0) : null); 98 } 99 100 //------------ 101 /** 102 * Returns the last node corresponding to an XPath 103 * 104 * @param string $xpath The xpath string 105 * @param DOMElement|null The search base(if null, starts from the root node) 106 * 107 * @returns DOMNode|null null if no match 108 */ 109 110 public function lastNode($xpath,$base=null) 111 { 112 $node_list=$this->nodes($xpath,$base); 113 return ($node_list->length ? $node_list->item($node_list->length-1) : null); 114 } 115 116 #-------- 117 /** 118 * Returns the values of the nodes corresponding to an XPath (nodeValue) 119 * 120 * @param string $xpath The xpath string 121 * @param DOMElement|null The search base(if null, starts from the root node) 122 * 123 * @returns array empty if no match 124 */ 125 126 public function values($xpath,$base=null) 127 { 128 $a=array(); 129 $res=$this->nodes($xpath,$base); 130 if (!$res->length) return $a; 131 foreach($res as $node) $a[]=$node->nodeValue; 132 return $a; 133 } 134 135 #-------- 136 /** 137 * Returns the value of the first node corresponding to an XPath (nodeValue) 138 * 139 * @param string $xpath The xpath string 140 * @param DOMElement|null The search base(if null, starts from the root node) 141 * 142 * @returns string|null null if no match 143 */ 144 145 public function value($xpath,$base=null) 146 { 147 $node=$this->node($xpath,$base); 148 149 return (($node===null) ? null : $node->nodeValue); 150 } 151 152 #-------- 153 /** 154 * Returns the number of nodes corresponding to an XPath (nodeValue) 155 * 156 * @param string $xpath The xpath string 157 * @param DOMElement|null The search base(if null, starts from the root node) 158 * 159 * @returns int 160 */ 161 162 public function nb($xpath,$base=null) 163 { 164 return $this->nodes($xpath,$base)->length; 165 } 166 167 #-------- 168 /** 169 * Returns the text and target of an hyperlink node as an 170 * array('text' => <text>, 'url' => <target>) 171 * 172 * @param DOMNode $node Node of type 'a' 173 * 174 * @returns array(text,url) 175 * 176 * @throws Exception 177 */ 178 179 public static function aInfo($node) 180 { 181 if ((! $node instanceof DOMElement)) 182 throw new \Exception('Arg should be a DOMElement'); 183 184 if ($node->tagName != 'a') 185 throw new \Exception('Node should be an anchor ('.$node->tagName.')'); 186 187 return array('text' => $node->textContent 188 ,'url' => $node->attributes->getNamedItem('href')->nodeValue); 189 } 190 191 #-------- 192 193 public function recursiveBackNodes($start_node,$item) 194 { 195 $a=array(); 196 $node=$start_node; 197 198 while (true) 199 { 200 $nodes=$this->nodes($item,$node); 201 if ($nodes->length) 202 { 203 for ($i=0;$i<$nodes->length;$i++) $a[]=$nodes->item($i); 204 } 205 if ($node->isSameNode($this->xdoc)) break; // if root node 206 $node=$node->parentNode; 207 } 208 return $a; 209 } 210 211 #-------- 212 213 public function recursiveBackNode($start_node,$item) 214 { 215 $nodes=$this->recursiveBackNodes($start_node,$item); 216 return ((count($nodes)==0) ? null : $nodes[0]); 217 } 218 219 #-------- 220 221 public function recursiveBackValues($start_node,$item) 222 { 223 $a=array(); 224 foreach ($this->recursiveBackNodes($start_node,$item) as $node) 225 $a[]=$node->nodeValue; 226 return $a; 227 } 228 229 #-------- 230 231 public function recursiveBackValue($start_node,$item) 232 { 233 $node=$this->recursiveBackNode($start_node,$item); 234 return (is_null($node) ? null : $node->nodeValue); 235 } 236 237 //---------- 238 } // End of class XPage 239 //============================================================================= 240 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jun 4 19:17:11 2015 | Cross-referenced by PHPXref 0.7.1 |