Miscellaneous Code

  Home arrow Miscellaneous Code arrow Simple XML Parser
MISCELLANEOUS CODE

Simple XML Parser
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2003-12-28

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    Simple XML Parser which reads XML files and create an array from the data

    By : koolb

    <?php
    // Author: Koolb, Buddhima W.Wickramsinghe (buddhima@yahoo.com)

    $arfiles= array("24122003_5_AccessoryList.xml","28122003_4_Shop_OtherModel.xml");

    foreach($arfiles as $arFile)
    {
    echo "<BR>-------------------------------------------------<BR>";
    echo "<BR>Parsing ".$arFile."<BR>";
    $insXmlParser= new clsXmlParser($arFile);
    if($aArray=$insXmlParser->Parse())
    echo LIST_CONTENTS($aArray);
    echo "<BR>-------------------------------------------------<BR>";
    }
    // Simple XML Parser

    class clsXmlParser {

    // general vars
    var $sTitle = "";
    var $sLink = "";
    var $sDescription = "";
    var $arItems = array();
    var $arsub = array();
    var $itemCount = 0;
    var $prvTag="";
    var $uFiles = '';
    var $xml_parser;
    var $curTag="";

    function clsXmlParser($uFiles)
    {

    $this->uFiles = $uFiles;

    }

    function startElement($parser, $name, $attrs) {

    $this->curTag .= "^$name";
    //echo "start: ".$this->curTag." <BR>";

    }

    function endElement($parser, $name)
    {

    $caret_pos = strrpos($this->curTag,'^');
    $this->curTag = substr($this->curTag,0,$caret_pos);
    //echo "end: ".$this->curTag." <BR>";
    }

    function characterData($parser, $data) {

    if(trim($data) != "")
    {
    if(trim($this->prvTag) == "")
    $this->prvTag=$this->curTag;
    elseif(trim($this->prvTag) == trim($this->curTag))
    {
    $this->arItems[] = $this->arsub;
    $this->arsub = array();

    }

    //find current element
    $c_pos = strrpos($this->curTag,'^');
    $c_len = strlen($this->curTag);
    $c_val = substr($this->curTag,($c_pos+1),$c_len);

    //set data to sub array with the element name as the key
    $this->arsub[$c_val] = $data;


    }

    }



    function Parse()
    {
    $this->xml_parser = xml_parser_create();


    xml_set_object($this->xml_parser, &$this);

    xml_set_element_handler($this->xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($this->xml_parser, "characterData");

    if (!($fp = fopen($this->uFiles,"r")))
    {
    die ("could not open XML for input");
    }

    while ($data = fread($fp, 4096))
    {
    if (!xml_parse($this->xml_parser, $data, feof($fp)))
    {
    die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser)));
    }
    }
    xml_parser_free($this->xml_parser);

    //to handle the last array element
    if(count($this->arsub)>0)
    {
    $this->arItems[] = $this->arsub;
    $this->arsub = array();
    }

    return $this->arItems;

    }

    }

    ///----------END OF CLASS


    function LIST_CONTENTS($arrayname,$tab="&nbsp&nbsp&nbsp&nbsp",$indent=0)
    {
    // recursively displays contents of the array and sub-arrays:
    // This function (c) Peter Kionga-Kamau
    // Free for unrestricted use, except sale - do not resell.
    // use: echo LIST_CONTENTS(array $arrayname, string $tab, int $indent);
    // $tab = string to use as a tab, $indent = number of tabs to indent result
    $retval=$currenttab="";
    while(list($key, $value) = each($arrayname))
    {
    for($i=0; $i<$indent; $i++) $currenttab .= $tab;
    if (is_array($value))
    {
    $retval .= "$currenttab$key : Array: <BR>$currenttab{<BR>";
    $retval .= LIST_CONTENTS($value,$tab,$indent+1)."$currenttab}<BR>";
    }
    else $retval .= "$currenttab$key => $value<BR>";
    $currenttab = NULL;
    }
    return $retval;
    }


    ?>

    Click to Download File



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap