Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezxml.php
Go to the documentation of this file.
1 <?php
3 //
4 // $Id: ezxml.php 8931 2002-01-11 18:58:09Z kaid $
5 //
6 // Definition of eZXML class
7 //
8 // B�rd Farstad <bf@ez.no>
9 // Created on: <16-Nov-2001 11:26:01 bf>
10 //
11 // This source file is part of Exponential Basic, publishing software.
12 // Copyright (C) 1999-2000 eZ systems as
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
27 //
28
31
41 // include_once( "ezxml/classes/ezdomnode.php" );
42 // include_once( "ezxml/classes/ezdomdocument.php" );
43
44 class eZXML
45 {
49 function __construct( )
50 {
51 print( "Use the static functions: eZXML::domTree()" );
52 }
53
60 static public function domTree( $xmlDoc, $params=array() )
61 {
62 $TagStack = array();
63
64 // get document version
65 // stip the !doctype (xdutoit modification)
66 $xmlDoc =& preg_replace( "%<\!DOCTYPE.*?>%is", "", $xmlDoc );
67
68 // strip header
69 $xmlDoc =& preg_replace( "#<\?.*?\?>#", "", $xmlDoc );
70
71 // strip comments
72 $xmlDoc =& eZXML::stripComments( $xmlDoc );
73
74
75 // libxml compatible object creation
76 $domDocument = new eZDOMDocument();
77 $domDocument->version = "1.0";
78
79 $domDocument->root =& $domDocument->children;
80 $currentNode =& $domDocument;
81
82 $pos = 0;
83 $endTagPos = 0;
84 while ( $pos < strlen( $xmlDoc ) )
85 {
86 $char = $xmlDoc[$pos];
87 if ( $char == "<" )
88 {
89 // find tag name
90 $endTagPos = strpos( $xmlDoc, ">", $pos );
91
92 // tag name with attributes
93 $tagName = substr( $xmlDoc, $pos + 1, $endTagPos - ( $pos + 1 ) );
94
95 // check if it's an endtag </tagname>
96 if ( $tagName[0] == "/" )
97 {
98 $lastNodeArray = array_pop( $TagStack );
99 $lastTag = $lastNodeArray["TagName"];
100
101 $lastNode =& $lastNodeArray["ParentNodeObject"];
102
103 unset( $currentNode );
104 $currentNode =& $lastNode;
105
106 $tagName = substr( $tagName, 1, strlen( $tagName ) );
107
108 // strip out namespace; nameSpace:Name
109 $colonPos = strpos( $tagName, ":" );
110
111 if ( $colonPos > 0 )
112 $tagName = substr( $tagName, $colonPos + 1, strlen( $tagName ) );
113
114 //gb: not understanding what this was original intended on doing so we disable it to proceed.
115 if ( $lastTag != $tagName )
116 {
117 print( "Error parsing XML, unmatched tags $tagName" );
118 return false;
119 }
120 else
121 {
122 // print( "endtag name: $tagName ending: $lastTag <br> " );
123 }
124 }
125 else
126 {
127 $firstSpaceEnd = strpos( $tagName, " " );
128 $firstNewlineEnd = strpos( $tagName, "\n" );
129
130 if ( $firstNewlineEnd != false )
131 {
132 if ( $firstSpaceEnd != false )
133 {
134 $tagNameEnd = min( $firstSpaceEnd, $firstNewlineEnd );
135 }
136 else
137 {
138
139 $tagNameEnd = $firstNewlineEnd;
140 }
141 }
142 else
143 {
144 if ( $firstSpaceEnd != false )
145 {
146 $tagNameEnd = $firstSpaceEnd;
147 }
148 else
149 {
150 $tagNameEnd = 0;
151 }
152 }
153
154 if ( $tagNameEnd > 0 )
155 {
156 $justName = substr( $tagName, 0, $tagNameEnd );
157 }
158 else
159 $justName = $tagName;
160
161
162 // strip out namespace; nameSpace:Name
163 $colonPos = strpos( $justName, ":" );
164
165 if ( $colonPos > 0 )
166 $justName = substr( $justName, $colonPos + 1, strlen( $justName ) );
167
168
169 // remove trailing / from the name if exists
170 if ( $justName[strlen($justName) - 1] == "/" )
171 {
172 $justName = substr( $justName, 0, strlen( $justName ) - 1 );
173 }
174
175
176 // check for CDATA
177 $cdataSection = "";
178 $isCDATASection = false;
179 $cdataPos = strpos( $xmlDoc, "<![CDATA[", $pos );
180 if ( $cdataPos == $pos && $pos > 0)
181 {
182 $isCDATASection = true;
183 $endTagPos = strpos( $xmlDoc, "]]>", $cdataPos );
184 $cdataSection =& substr( $xmlDoc, $cdataPos + 9, $endTagPos - ( $cdataPos + 9 ) );
185
186 // new CDATA node
187 unset( $subNode );
188 $subNode = new eZDOMNode();
189 $subNode->name = "cdata-section";
190 $subNode->content = $cdataSection;
191 $subNode->type = 4;
192
193 $currentNode->children[] =& $subNode;
194
195 $pos = $endTagPos;
196 $endTagPos += 2;
197
198 }
199 else
200 {
201 // normal start tag
202 unset( $subNode );
203 $subNode = new eZDOMNode();
204 $subNode->name = $justName;
205 $subNode->type = 1;
206
207 $currentNode->children[] =& $subNode;
208 }
209
210 // find attributes
211 if ( $tagNameEnd > 0 )
212 {
213 $attributePart =& substr( $tagName, $tagNameEnd, strlen( $tagName ) );
214
215 // attributes
216 unset( $attr );
217 $attr =& eZXML::parseAttributes( $attributePart );
218
219 if ( $attr != false )
220 $subNode->attributes =& $attr;
221 }
222
223 // check it it's a oneliner: <tagname /> or a cdata section
224 if ( $isCDATASection == false )
225 if ( $tagName[strlen($tagName) - 1] != "/" )
226 {
227 array_push( $TagStack,
228 array( "TagName" => $justName, "ParentNodeObject" => &$currentNode ) );
229
230 unset( $currentNode );
231 $currentNode =& $subNode;
232 }
233 }
234 }
235
236 $pos = strpos( $xmlDoc, "<", $pos + 1 );
237
238 if ( $pos == false )
239 {
240 // end of document
241 $pos = strlen( $xmlDoc );
242 }
243 else
244 {
245 // content tag
246 $tagContent = substr( $xmlDoc, $endTagPos + 1, $pos - ( $endTagPos + 1 ) );
247
248 if ( !isset( $params["TrimWhiteSpace"] ) )
249 $params["TrimWhiteSpace"] = false;
250
251 if ( ( ( $params["TrimWhiteSpace"] == true ) and ( trim( $tagContent ) != "" ) ) or ( $params["TrimWhiteSpace"] == false ) )
252 {
253 unset( $subNode );
254 $subNode = new eZDOMNode();
255 $subNode->name = "text";
256 $subNode->type = 3;
257
258 // convert special chars
259 $tagContent =& str_replace("&gt;", ">", $tagContent );
260 $tagContent =& str_replace("&lt;", "<", $tagContent );
261 $tagContent =& str_replace("&apos;", "'", $tagContent );
262 $tagContent =& str_replace("&quot;", '"', $tagContent );
263 $tagContent =& str_replace("&amp;", "&", $tagContent );
264
265 $subNode->content = $tagContent;
266
267 $currentNode->children[] =& $subNode;
268 }
269 }
270 }
271
272 return $domDocument;
273 }
274
279 static public function stripComments( &$str )
280 {
281 $str =& preg_replace( "#<\!--.*?-->#s", "", $str );
282 return $str;
283 }
284
290 static public function &parseAttributes( $attributeString )
291 {
292 $ret = array();
293
294 preg_match_all( "/([a-zA-Z:]+=\".*?\")/i", $attributeString, $attributeArray );
295
296 foreach ( $attributeArray[0] as $attributePart )
297 {
298 $attributePart = $attributePart;
299
300 if ( trim( $attributePart ) != "" && trim( $attributePart ) != "/" )
301 {
302 $attributeTmpArray = explode( "=\"", $attributePart );
303
304 $attributeName = $attributeTmpArray[0];
305
306 // strip out namespace; nameSpace:Name
307 $colonPos = strpos( $attributeName, ":" );
308
309 if ( $colonPos > 0 )
310 $attributeName = substr( $attributeName, $colonPos + 1, strlen( $attributeName ) );
311
312 $attributeValue = $attributeTmpArray[1];
313
314 // remove " from value part
315 $attributeValue = substr( $attributeValue, 0, strlen( $attributeValue ) - 1);
316
317 unset( $attrNode );
318 $attrNode = new eZDOMNode();
319 $attrNode->name = $attributeName;
320 $attrNode->type = 2;
321 $attrNode->content = $attributeValue;
322
323 unset( $nodeValue );
324 $nodeValue = new eZDOMNode();
325 $nodeValue->name = "text";
326 $nodeValue->type = 3;
327 $nodeValue->content = $attributeValue;
328
329 $attrNode->children[] =& $nodeValue;
330
331 $ret[] =& $attrNode;
332
333 }
334 }
335 return $ret;
336 }
337
338 }
339
340 ?>
eZDOMDocument
Definition: ezdomdocument.php:35
eZDOMNode
Definition: ezdomnode.php:35
eZXML
Definition: ezxml.php:45
eZXML\__construct
__construct()
Definition: ezxml.php:49
eZXML\parseAttributes
static & parseAttributes( $attributeString)
Definition: ezxml.php:290
eZXML\stripComments
static stripComments(&$str)
Definition: ezxml.php:279
eZXML\domTree
static domTree( $xmlDoc, $params=array())
Definition: ezxml.php:60
$str
$str
Definition: ezarticle/modules/content.php:30
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.