1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384:
<?php
require_once('AWLUtilities.php');
class XMLElement {
protected $tagname;
protected $xmlns;
protected $attributes;
protected $content;
protected $_parent;
function __construct( $tagname, $content=false, $attributes=false, $xmlns=null ) {
$this->tagname=$tagname;
if ( gettype($content) == "object" ) {
$this->content = array(&$content);
}
else {
$this->content = $content;
}
$this->attributes = $attributes;
if ( isset($xmlns) ) {
$this->xmlns = $xmlns;
}
else {
if ( preg_match( '{^(.*):([^:]*)$}', $tagname, $matches) ) {
$prefix = $matches[1];
$tag = $matches[2];
if ( isset($this->attributes['xmlns:'.$prefix]) ) {
$this->xmlns = $this->attributes['xmlns:'.$prefix];
}
}
else if ( isset($this->attributes['xmlns']) ) {
$this->xmlns = $this->attributes['xmlns'];
}
}
}
function CountElements( ) {
if ( $this->content === false ) return 0;
if ( is_array($this->content) ) return count($this->content);
if ( $this->content == '' ) return 0;
return 1;
}
function SetAttribute($k,$v) {
if ( gettype($this->attributes) != "array" ) $this->attributes = array();
$this->attributes[$k] = $v;
if ( strtolower($k) == 'xmlns' ) {
$this->xmlns = $v;
}
}
function SetContent($v) {
$this->content = $v;
}
function GetTag() {
return $this->tagname;
}
function GetNSTag() {
return (empty($this->xmlns) ? '' : $this->xmlns . ':') . $this->tagname;
}
function GetAttribute( $attr ) {
if ( $attr == 'xmlns' ) return $this->xmlns;
if ( isset($this->attributes[$attr]) ) return $this->attributes[$attr];
return null;
}
function GetAttributes() {
return $this->attributes;
}
function GetContent() {
return $this->content;
}
function GetElements( $tag=null, $recursive=false ) {
$elements = array();
if ( gettype($this->content) == "array" ) {
foreach( $this->content AS $k => $v ) {
if ( empty($tag) || $v->GetNSTag() == $tag ) {
$elements[] = $v;
}
if ( $recursive ) {
$elements = $elements + $v->GetElements($tag,true);
}
}
}
else if ( empty($tag) || (isset($v->content->tagname) && $v->content->GetNSTag() == $tag) ) {
$elements[] = $this->content;
}
return $elements;
}
function GetPath( $path ) {
$elements = array();
if ( !preg_match( '#(/)?([^/]+)(/?.*)$#', $path, $matches ) ) return $elements;
if ( $matches[2] == '*' || $matches[2] == $this->GetNSTag()) {
if ( $matches[3] == '' ) {
$elements[] = $this;
}
else if ( gettype($this->content) == "array" ) {
foreach( $this->content AS $k => $v ) {
$elements = array_merge( $elements, $v->GetPath($matches[3]) );
}
}
}
if ( $matches[1] != '/' && gettype($this->content) == "array" ) {
foreach( $this->content AS $k => $v ) {
$elements = array_merge( $elements, $v->GetPath($path) );
}
}
return $elements;
}
function AddSubTag(&$v) {
if ( gettype($this->content) != "array" ) $this->content = array();
$this->content[] =& $v;
return count($this->content);
}
function &NewElement( $tagname, $content=false, $attributes=false, $xmlns=null ) {
if ( gettype($this->content) != "array" ) $this->content = array();
$element = new XMLElement($tagname,$content,$attributes,$xmlns);
$this->content[] =& $element;
return $element;
}
function RenderContent($indent=0, $nslist=null, $force_xmlns=false ) {
$r = "";
if ( is_array($this->content) ) {
$r .= "\n";
foreach( $this->content AS $k => $v ) {
if ( is_object($v) ) {
$r .= $v->Render($indent+1, "", $nslist, $force_xmlns);
}
}
$r .= substr(" ",0,$indent);
}
else {
if(strpos($this->content, '<![CDATA[')===0 && strrpos($this->content, ']]>')===strlen($this->content)-3)
$r .= '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', substr($this->content, 9, -3)) . ']]>';
else if ( defined('ENT_XML1') && defined('ENT_DISALLOWED') )
$r .= htmlspecialchars($this->content, ENT_NOQUOTES | ENT_XML1 | ENT_DISALLOWED );
else
$r .= htmlspecialchars($this->content, ENT_NOQUOTES );
}
return $r;
}
function Render($indent=0, $xmldef="", $nslist=null, $force_xmlns=false) {
$r = ( $xmldef == "" ? "" : $xmldef."\n");
$attr = "";
$tagname = $this->tagname;
$xmlns_done = false;
if ( gettype($this->attributes) == "array" ) {
foreach( $this->attributes AS $k => $v ) {
if ( preg_match('#^xmlns(:?(.+))?$#', $k, $matches ) ) {
if ( !isset($nslist) ) $nslist = array();
$prefix = (isset($matches[2]) ? $matches[2] : '');
if ( isset($nslist[$v]) && $nslist[$v] == $prefix ) continue;
$nslist[$v] = $prefix;
if ( !isset($this->xmlns) ) $this->xmlns = $v;
$xmlns_done = true;
}
$attr .= sprintf( ' %s="%s"', $k, htmlspecialchars($v) );
}
}
if ( isset($this->xmlns) && isset($nslist[$this->xmlns]) && $nslist[$this->xmlns] != '' ) {
$tagname = $nslist[$this->xmlns] . ':' . $tagname;
if ( $force_xmlns ) $attr .= sprintf( ' xmlns="%s"', $this->xmlns);
}
else if ( isset($this->xmlns) && !isset($nslist[$this->xmlns]) && gettype($this->attributes) == 'array' && !isset($this->attributes[$this->xmlns]) ) {
$attr .= sprintf( ' xmlns="%s"', $this->xmlns);
}
else if ( $force_xmlns && isset($this->xmlns) && ! $xmlns_done ) {
$attr .= sprintf( ' xmlns="%s"', $this->xmlns);
}
$r .= substr(" ",0,$indent) . '<' . $tagname . $attr;
if ( (is_array($this->content) && count($this->content) > 0) || (!is_array($this->content) && strlen($this->content) > 0) ) {
$r .= ">";
$r .= $this->RenderContent($indent,$nslist,$force_xmlns);
$r .= '</' . $tagname.">\n";
}
else {
$r .= "/>\n";
}
return $r;
}
function __tostring() {
return $this->Render();
}
}
function BuildXMLTree( $xmltags, &$start_from ) {
$content = array();
if ( !isset($start_from) ) $start_from = 0;
for( $i=0; $i < 50000 && isset($xmltags[$start_from]); $i++) {
$tagdata = $xmltags[$start_from++];
if ( !isset($tagdata) || !isset($tagdata['tag']) || !isset($tagdata['type']) ) break;
if ( $tagdata['type'] == "close" ) break;
$xmlns = null;
$tag = $tagdata['tag'];
if ( preg_match( '{^(.*):([^:]*)$}', $tag, $matches) ) {
$xmlns = $matches[1];
$tag = $matches[2];
}
$attributes = ( isset($tagdata['attributes']) ? $tagdata['attributes'] : false );
if ( $tagdata['type'] == "open" ) {
$subtree = BuildXMLTree( $xmltags, $start_from );
$content[] = new XMLElement($tag, $subtree, $attributes, $xmlns );
}
else if ( $tagdata['type'] == "complete" ) {
$value = ( isset($tagdata['value']) ? $tagdata['value'] : false );
$content[] = new XMLElement($tag, $value, $attributes, $xmlns );
}
}
if ( count($content) == 1 ) {
return $content[0];
}
return $content;
}