SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: XML in PHP 4 !

  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML in PHP 4 !

    Hi
    How can I convert this code to PHP4 ?

    Code:
     <?php
      $data = array();
      
      
      if ( $_FILES['file']['tmp_name'] )
      {
      	$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
      	$rows = $dom->getElementsByTagName( 'Row' );
      	$first_row = true;
      	foreach ($rows as $row)
      	{
     	 	if ( !$first_row )
     	 	{
    			  $name = "";
    			  $family = "";
    			  $grade = "";
    			  $shsh = "";
      
     			 $index = 1;
     			 $cells = $row->getElementsByTagName( 'Cell' );
     			 foreach( $cells as $cell )
     			 { 
     				 $ind = $cell->getAttribute( 'Index' );
     				 if ( $ind != null ) $index = $ind;
    
      				if ( $index == 1 ) $name = $cell->nodeValue;
      				if ( $index == 2 ) $family = $cell->nodeValue;
      				if ( $index == 3 ) $grade = $cell->nodeValue;
      				if ( $index == 4 ) $shsh = $cell->nodeValue;
      
     				 $index += 1;
     			 }
    
     		  	$data []= array(
      				'Name' => $name,
      				'Family' => $family,
      				'Grade' => $grade,
      				'Shsh' => $shsh 
      				);
     	 }
      	$first_row = false;
      }
      }
      ?>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,049
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    version 4

    I question the wisdom of writing something for version 4 when version 6 is on the way. Anyway, for PHP version 4 you need to use the DOM XML functions http://us.php.net/manual/en/ref.domxml.php

  3. #3
    SitePoint Enthusiast tjk's Avatar
    Join Date
    May 2006
    Location
    Melbourne, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes mittineague whilst PHP 6 is on the way, not all hosts have fully upgraded to PHP 5, mine included (unfortunately!).

    I've recently been trying to parse XML using PHP 4, so bakhanbeigi maybe this link will help

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,049
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    parser

    That tutorial is about working with the expat library by James Clark.
    If you feel that is OK, then you can skip the options.
    But IMHO if something provides for an improvement, then it is best to test for it's support before falling back to a less-than-optimal alternate.
    ie. compatible rather than converted.

  5. #5
    SitePoint Enthusiast tjk's Avatar
    Join Date
    May 2006
    Location
    Melbourne, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mittineague

    I'm fairly new to this sort of PHP work. Are you saying it is best to work with the DOM XML functions (I see there are a number deprecated functions that have been replaced) rather than the technique used in the link I posted earlier?

    I'll try and sink my teeth into the DOM XML.

    Tim

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,049
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    xml parsing

    I still have code like that (expat lib). And it still works for what I need it to do. It's not as "powerful" perhaps, but I don't need it to do more.
    However, if you get into working with XML (SOAP, AJAX, etc etc) you may find that you need the additional capabilities and/or want the OOP of it.
    bakhanbeigi's posted code uses "DOMDocument" - PHP 5's DOM functions.
    As you mentioned some of PHP 4's DOM XML (GNOME lib) functions are deprecated, so it would be best to avoid using those.
    I guess it depends on what your code is doing, but if you use
    array DomDocument->get_elements_by_tagname ( string $name )
    then it will not be compatible with PHP version 5
    DOMNodeList getElementsByTagName ( string $name )

  7. #7
    SitePoint Enthusiast tjk's Avatar
    Join Date
    May 2006
    Location
    Melbourne, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep I see what you mean now.

    Thanks Mittineague

  8. #8
    SitePoint Member
    Join Date
    Oct 2007
    Location
    India
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    $data = array();

    This will work in PHP4

    if ( $_FILES['file']['tmp_name'] )
    {
    $dom = domxml_open_mem( $_FILES['file']['tmp_name'] );
    $rows = $dom->get_elements_by_tagname( 'Row' );
    $first_row = true;
    foreach ($rows as $row)
    {
    if ( !$first_row )
    {
    $name = "";
    $family = "";
    $grade = "";
    $shsh = "";

    $index = 1;
    $cells = $row->get_elements_by_tagname( 'Cell' );
    foreach( $cells as $cell )
    {
    $ind = $cell[0]->get_attribute( 'Index' );
    if ( $ind != null ) $index = $ind;

    if ( $index == 1 ) $name = $cell[0]->get_content();
    if ( $index == 2 ) $family = $cell[0]->get_content();
    if ( $index == 3 ) $grade = $cell[0]->get_content();
    if ( $index == 4 ) $shsh = $cell[0]->get_content();

    $index += 1;
    }

    $data []= array(
    'Name' => $name,
    'Family' => $family,
    'Grade' => $grade,
    'Shsh' => $shsh
    );
    }
    $first_row = false;
    }
    }
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •