note 21350 deleted from ref.xml by jimw
| From: | jimw@php.net | Date: | Fri, 10 May 2002 19:18:28 +0000 |
| Subject: | note 21350 deleted from ref.xml by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-30436@lists.php.net to get a copy of this message | ||
<?php
/***
*
* XML_Document
* A class for an XML document
*
* (c) 2002 Henrik Gebauer
* mailto:henrik.gebauer@web.de
*
***/
// class for an XML document
class XML_Document
{
var $xml_string = '';
var $filename = '';
var $root_element = '';
// These variables are used by the class itself
var $_vals;
var $_i = 0;
/* Constructor
* @param $data the filename or the string to parse
* @param $type says whether $data is a file ('file') or a string
* ('string').
*/
function XML_Document($data, $type = 'file')
{
// $data is the filename
if($type == 'file')
{
$this->filename = $data;
$this->xml_string = implode('', file($this->filename));
}
// $data is a string
else if($type == 'string')
{
$this->filename = '';
$this->xml_string = $data;
}
// Invalid value for $type -> Error
else
{
die('Invalid value vor $type in XML_Document::XML_Document()');
}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $this->xml_string, &$this->_vals, &$index);
xml_parser_free($parser);
// Save the root element
$this->root_element = $this->_get_element();
}
// returns the root element
function root()
{
return $this->root_element;
}
// returns the filename element
function filename()
{
return $this->filename;
}
// returns the xml-code
function xml()
{
return $this->xml_string;
}
/***
* Returns an element with all its sub-elements
*
* @return
* Array(
* 'level' Level (1 is the root element)
* 'tag' name of the atg
* 'type' type of the tag('open' = contains sub elements,
* 'complete' = contains only text
* 'attributes' Attributes of the element
* 'value' Text of the element(type = 'complete')
* 'children' Sub elements
* )
*
* THIS FUNCTION SHOULD ONLY BE USED BY THE CLASS ITSELF!
*/
function _get_element()
{
$level = $this->_vals[$this->_i]['level'];
$tag = strtolower($this->_vals[$this->_i]['tag']);
$type = $this->_vals[$this->_i]['type'];
$value = isset($this->_vals[$this->_i]['value']) ?
trim($this->_vals[$this->_i]['value']) : null;
$children = array();
if(isset($this->_vals[$this->_i]['attributes']))
{
$attributes = array();
foreach($this->_vals[$this->_i]['attributes'] as $attribute=>$v)
$attributes[strtolower($attribute)] = $v;
}
else
{
$attributes = array();
}
$element = array(
'level' => &$level,
'tag' => &$tag,
'type' => &$type,
'attributes' => &$attributes,
'value' => &$value,
'children' => &$children
);
if($type == 'open')
{
while(++$this->_i < count($this->_vals))
{
if($this->_vals[$this->_i]['level'] == $level &&
$this->_vals[$this->_i]['type'] == 'close')
break;
else if($this->_vals[$this->_i]['type'] == 'cdata')
$children[] = trim($this->_vals[$this->_i]['value']);
else if($this->_vals[$this->_i]['type'] != 'close')
$children[strtolower($this->_vals[$this->_i]['tag'])][] =
$this->_get_element($this->_vals, $this->_i);
}
}
return $element;
}
};
?>