A script that mimics SimpleXML in PHP5

<?php
function ws_parse_xml_complete($xml_file,$sort)
{
        #####> start the xml parser
       $xml_file_string = file_get_contents($xml_file);
        $xml_parser = xml_parser_create();
        xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
        xml_parse_into_struct($xml_parser, $xml_file_string, $a_vals, $a_index);
        xml_parser_free($xml_parser);

        $Group_cnt = $rec_cnt = $loop_cnt = 0;
        foreach($a_vals as $a_val) #####> loop through the entire array of the xml file
       {
                if($a_val['type'] == 'open') #####> if a tag is open, see if it's <Group> or <rec>
               {
                        if($a_val['tag'] == 'Group')
                        {
                                $Group_type = 'open';
                        }
                        elseif($a_val['tag'] == 'rec')
                        {
                                $rec_type = 'open';
                                $rec_Flag_cnt = $loop_cnt + 1;
                                $rec_campus_cnt = $loop_cnt + 7;
                        }
                }

                if($xml_parsed->document->Group[$Group_cnt]->alphabet == '' && $Group_type == 'open')
                {
                        $xml_parsed->document->Group[$Group_cnt]->alphabet = $a_val['value'];
                }

                if($rec_type == 'open')
                {
                        if(($sort == 'complete' || $sort == '' || $sort == $a_vals[$rec_campus_cnt]['value']) && ($a_vals[$rec_Flag_cnt]['value'] == 1 || $a_vals[$rec_Flag_cnt]['value'] == 3))
                        {
                                $a_tags = array("Flag","Name","phone","email","role","location","campus","gName");

                                foreach($a_tags as $tag)
                                {
                                        if($a_val['tag'] == $tag)
                                        {
                                                $xml_parsed->document->Group[$Group_cnt]->rec[$rec_cnt]->{$tag} = $a_val['value'];
                                        }
                                }
                        }
                }

                if($a_val['type'] == 'close')
                {
                        if($a_val['tag'] == 'Group')
                        {
                $Group_type = 'close';
                                $Group_cnt += 1;
                                $rec_cnt = 0;
                }
                elseif($a_val['tag'] == 'rec')
                        {
                $rec_type = 'close';
                                $rec_cnt += 1;
                }
                }

                $loop_cnt += 1;
        }

        return $xml_parsed;
}
?>

Below is the general structure of my XML file.

<List> <!--This is ''document'' in the script-->
  <Group>
    <alphabet>A</alphabet>
    <rec>
      <Flag>1</Flag>
      <Name>Able, Foo</Name>
      <phone>555-555-5555</phone>
      <email>joefoo@foonannyfoo.bar</email>
      <role>Master</role>
      <location>Right Here</location>
      <campus>Harvard</campus>
      <gName>Whatever you want</gName>
    </rec>
  </Group>
</List>

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.