note 21145 deleted from ref.xml by jimw
| From: | jimw@php.net | Date: | Fri, 10 May 2002 19:18:18 +0000 |
| Subject: | note 21145 deleted from ref.xml by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-30435@lists.php.net to get a copy of this message | ||
/***
*
* XML_Document
* A class for an XML document
*
* (c) 2002 Henrik Gebauer
* mailto:henrik.gebauer@web.de
*
***/
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 filename or string, which has to be parsed
* @param $type says whether $data is a file ('file') or a string
* ('string').
*/
function XML_Document($data, $type = 'file')
{
if($type == 'file')
{
$this->filename = $data;
$this->xml_string
$this->xml_string = implode('', file($this->filename));
}
else if($type == 'string')
{
$this->filename = '';
>xml_string
$this->xml_string = $data;
}
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);
$this->root_element = $this->_get_element();
}
function root()
{
return $this->root_element;
}
function filename()
{
return $this->filename;
}
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 FUNCTION 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;
}
};