Link to home
Start Free TrialLog in
Avatar of boodabelly
boodabelly

asked on

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
Avatar of jdpipe
jdpipe
Flag of Australia image

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
ASKER CERTIFIED SOLUTION
Avatar of mensuck
mensuck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boodabelly
boodabelly

ASKER

Suzanne,

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

Thanks
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
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.
my bad, error in my XML file, works wonderfully, comment will be accepted as answer