Parsing XML file

I have the following XML file structure and I am having problems parsing the file into a data structure
http://pastebin.com/283508

the PHP that I have written so far is
http://pastebin.com/283509

I have stepped through the code and the entire file is read in, but it does not appear to be parsing the data into a structure correctly.  Any ideas with what is going on here?

TIA
LVL 1
boodabellyAsked:
Who is Participating?
 
mensuckCommented:
Hi

What I normal do is just build a generic XML function that can parse any type of XML data, sort of like pre-processor. This sets the XML data into a simple multi dimensional array that allows me to create a simple back end function that can read through the structure array! By doing this I only need to use (2) functions that can handle 100(s) of different data feeds from different sources (weather, stocks, news, mailboxes, ....) without making changes to the core decoder, again and again!


(2) examples

1. your data example...

http://24.91.149.80/get.php

2. weather feed from noaa

http://24.91.149.80/weather.php

Both of these examples use the same include file that contains the (2) functions that handle both different XML type data trees! You can download that include file...

http://24.91.149.80/xml_get.txt

Keep a copy of it in your library, because like said these (2) functions can decode even the most difficult XML files.

To use these functions, just pass the XML file stream to....

function process_xml();

It will return the structure array!

Suzanne



0
 
jdpipeCommented:
Can't test this...

Warning: mysql_pconnect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111) in /home/pastebin/lib/pastebin/db.mysql.class.php on line 42
Unable to connect to database
0
 
boodabellyAuthor Commented:
Suzanne,

Would you mind using pastebin to post the code for the xml_get function?  The txt file is not working.

Thanks
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
mensuckCommented:
Hi

Here is the xml_get.php include file!

<?

// if not called by mom exit!

if ( !defined ( 'IS_INCLUDED' ) )
{
      exit ();
}

// this will begin the XML paser
// it looks for a certain structure and prosess the feed based on what
// type of structure it has found!


function process_xml ( $in )
{
      $xml = xml_parser_create ( 'ISO-8859-1' );
      xml_parser_set_option ( $xml, XML_OPTION_SKIP_WHITE, 1 );
      xml_parse_into_struct ( $xml, $in, $value, $name );
      xml_parser_free ( $xml );

      $x = 0;

      $part = array ();

      if ( !empty ( $value[$x]['attributes'] ) )
      {
            $part[$value[$x]['tag']][]['ATTRIBUTES'] = $value[$x]['attributes'];
            $name = ( count ( $part[$value[$x]['tag']] ) - 1 );
              $part[$value[$x]['tag']][$name] =  array_merge ( $part[$value[$x]['tag']][$name], get_extended ( $value, $x ) );
      }
      else
      {
            $part[$value[$x]['tag']][] = get_extended ( $value, $x );
      }

      return ( $part );
}

// this is called by process_xml and it is a recursive function that processes
// each part of the XML feed, if the array is multi demensional it recalls it's
// self so that it can merge each multi demensional array into a single element
// type array! There are a few other ways you can do this, but keeping it generic
// will allow you to do (2) things, (1) use the least amout of server resources
// and (2), be able to use these 2 same functions to process any XML feed you want!

function get_extended ( $value, &$x )
{
      $node = array ();

        if ( !empty ( $value[$x]['value'] ) )
      {
            $node['VALUE'] = $value[$x]['value'];
      }

        while ( ++$x < count ( $value ) )
      {
            switch ( $value[$x]['type'] )
            {
                  case 'cdata':
                        if ( !empty ( $node['VALUE'] ) )
                        {
                              $node['VALUE'] .= $value[$x]['value'];
                        }
                        else
                        {
                              $node['VALUE'] = $value[$x]['value'];
                        }
                  break;
                  case 'complete':
                        if ( !empty ( $value[$x]['attributes'] ) )
                        {
                              $node[$value[$x]['tag']][]['ATTRIBUTES'] = $value[$x]['attributes'];
                                  $name = ( count ( $node[$value[$x]['tag']] ) - 1 );
                              $node[$value[$x]['tag']][$name]['VALUE'] = !empty ( $value[$x]['value'] ) ? $value[$x]['value'] : '';
                        }
                        else
                        {
                              $node[$value[$x]['tag']][]['VALUE'] = !empty ( $value[$x]['value'] ) ? $value[$x]['value'] : '';
                        }
                  break;
                  case 'open':
                        if ( !empty ( $value[$x]['attributes'] ) )
                        {
                              $node[$value[$x]['tag']][]['ATTRIBUTES'] = $value[$x]['attributes'];
                              $name = ( count ( $node[$value[$x]['tag']] ) - 1 );
                              $node[$value[$x]['tag']][$name] = array_merge ( $node[$value[$x]['tag']][$name], get_extended ( $value, $x ) );
                        }
                        else
                        {
                              $node[$value[$x]['tag']][] = get_extended ( $value, $x );
                        }

                  break;
                  case 'close':
                  return ( $node );
            }
      }
}

?>


Suzanne
0
 
boodabellyAuthor Commented:
Suzanne,

What am I doing incorrectly here?

function XML_Read($file_name)
{
      // open the XML file and read the contents
      $fp = fopen($file_name, "r");
      
      if(!($fp))
      {
            // an error occured opening the file
            die("unable to process XML file");
      }

      // read the contents of the XML file
      $xml_contents = fread($fp, filesize($file_name));
      fclose($fp);
      $rev_val process_xml($xml_contents);
      return $ret_val
}

It is not returning the same stucture that you displayed.
0
 
boodabellyAuthor Commented:
my bad, error in my XML file, works wonderfully, comment will be accepted as answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.