Parsing a XML document in PHP scripting language
<!-- Parsing a XML document in PHP scripting language -->
<?php
class XmlParser {
private $document;
private $content;
private $xml_parser;
private $keys;
private $values;
private $key;
private $value;
private $key1;
private $value1;
private $tab;
private $line;
private $xml;
function __construct($arg)
{
$this->document = $arg;
$this->content = file_get_contents($this->document);
$this->xml_parser = xml_parser_create();
xml_parse_into_struct($this->xml_parser, $this->content, $this->values, $this->keys);
$this->tab = ' ';
$this->xml = '<?xml version="1.0" encoding="UTF-8"?>' . '<br />' . "\r\n" . '<br />' . "\r\n";
}
public function xml()
{
while( list($this->key, $this->value) = each($this->values) )
{
if($this->value['type'] == 'open')
{
$this->line = '<' . strtolower($this->value['tag']);
if(isset($this->value['attributes']))
{
if( is_array($this->value['attributes']) )
{
while( list($this->key1, $this->value1) = each($this->value['attributes']) )
{
$this->line .= ' ' . strtolower($this->key1) . '="' . $this->value1 . '"';
}
}
}
$this->line .= '>';
}
if($this->value['type'] == 'complete')
{
if(empty($this->value['value']))
{
$this->value['value'] = '';
}
$this->line = '<' . strtolower($this->value['tag']) . '>' . $this->value['value'] . '</' . strtolower($this->value['tag']) . '>';
}
if($this->value['type'] == 'close')
{
$this->line = '</' . strtolower($this->value['tag']) . '>';
}
if($this->value['level'] == 2)
{
$this->line = $this->tab . $this->line;
}
if($this->value['level'] == 3)
{
$this->line = $this->tab . $this->tab . $this->line;
}
if($this->value['level'] == 4)
{
$this->line = $this->tab . $this->tab . $this->tab . $this->line;
}
if($this->value['level'] == 5)
{
$this->line = $this->tab . $this->tab . $this->tab . $this->tab . $this->line;
}
if($this->value['type'] == 'open' || $this->value['type'] == 'complete' || $this->value['type'] == 'close')
{
$this->xml .= $this->line . '<br />' . "\r\n";
}
}
echo $this->xml;
}
function __destruct()
{
xml_parser_free($this->xml_parser);
}
}
$xml_parser = new XmlParser('links.xml');
$xml_parser->xml();
?>
<!-- mediasworks.org -->
<!-- mediasworks Group, India and worldwide -->
<!-- mediasworks nonprofit public participation project -->