Send this link to a friend by e-mail  |  Print this Web page  |  HOME  |  SCRIPTS  |  TUTORIALS  |  BACK  |  FORWARD  |  BOTTOM 

Parsing a XML document in PHP scripting language


PHP


<!-- 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 = '&nbsp; &nbsp; ';
        $this->xml = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;' . '<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 = '&lt;' . 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 .= '&gt;';
            }
            if($this->value['type'] == 'complete')
            {
                if(empty($this->value['value']))
                {
                    $this->value['value'] = '';
                }
                $this->line = '&lt;' . strtolower($this->value['tag']) . '&gt;' . $this->value['value'] . '&lt;/' . strtolower($this->value['tag']) . '&gt;';
            }
            if($this->value['type'] == 'close')
            {
                $this->line = '&lt;/' . strtolower($this->value['tag']) . '&gt;';
            }
            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 -->



Contribution is welcome, submit scripts or codes to mediasworks.org.

Donate for a cause


Technology Simplified!

Send this link to a friend by e-mail  |  TECHNOLOGY FORUMS  |  BACK  |  FORWARD  |  TOP  
Home | Theme | Contact | Feedback | E-mail | Advertise | Help | Privacy |   SiteMap