Hello to all,

I'm parsing a XML document into HML using PHP. The problem is that when I run the PHP script I get severals warnings messages when the fputs function in use, like this one:

Warning: fputs(): supplied argument is not a valid stream resource in /srv/www/htdocs/my_examples/script.php on line 50

If I use the echo sentence to write the result on the screen, instead of witting to a local file with fputs, no error or warning messages are reported, but the problem is that I have to use the fputs funtion to make my on html file for later use.

Do you have any ideas to solve this problem? I hope you can help me?

Many thanks.

MY PHP SCRIPT to parse the xml file


<?php
    //Initialize Parser
    $parser=xml_parser_create();
    //Specify Handlers to start and ending tag
    xml_set_element_handler($parser, "start_element", "end_element");
    //Data Handler
    xml_set_character_data_handler($parser, "character_data");
    //Open the Data File 
    $fp=fopen("script.xml", "r") ;

// Give a name to the html file
    $createdname='xFactura';
    $date=date("Ymd");
    $time=date("His");
    $source_file=$createdname.$date.$time.".html";
    $name_html=$source_file;    
//Open the html file for writing $fphtml=fopen($name_html,"a"); //Write a line. fputs($fphtml,'<html><body>'); //Small delay to write the data usleep(10000); //read the XML file Data while ($data=fread($fp, 4096)) { xml_parse($parser, $data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } xml_parser_free($parser); function start_element($parser, $element_name, $element_attrs) { switch($element_name) { case "SENDER": //echo ('<table border="1"><tbody>'); fputs($fphtml,'<table border="1"><tbody>');usleep(10000); break; case "SENDER_NAME": //echo ('<tr><td colspan="4">'); fputs($fphtml,'<tr><td colspan="4">');usleep(10000); break; case "ADDRESS": //echo ('<tr><td colspan="4">'); fputs($fphtml,'<tr><td colspan="4">');usleep(10000); break; case "CP_SENDER": //echo "<tr><td>"; fputs($fphtml,'<tr><td>');usleep(10000); break;
case "CYTY": //echo "<td>"; fputs($fphtml,'<td>');usleep(10000); break; case "REGION": //echo "<td>("; fputs($fphtml,'<td>(');usleep(10000); break; case "CIF_NUMBER": //echo ('<tr><td colspan="4">CIF:'); fputs($fphtml,'<tr><td colspan="4">CIF:');usleep(10000); break; case "PHONE_NUMEBR": //echo "<tr><td></td><td>Tel:";
fputs($fphtml,'<tr><td></td><td>Tel:');usleep(10000); break; case "FAX_NUMBER": //echo "<td>Fax:"; fputs($fphtml,'<td>Fax:');usleep(10000); break; } } function end_element($parser, $element_name) { switch($element_name) { case "SENDER": //echo "</tbody></table>"; fputs($fphtml,'</tbody></table>');usleep(10000); break; case "SENDER_NAME": //echo "</td></tr>"; fputs($fphtml,'</td></tr>');usleep(10000); break; case "ADDRESS": //echo "</td></tr>"; fputs($fphtml,'</td></tr>');usleep(10000); break; case "CP_SENDER": //echo "</td>"; fputs($fphtml,'</td>');usleep(10000); break;
case "CYTY": //echo "</td>"; fputs($fphtml,'</td>');usleep(10000); break; case "REGION": //echo ")</td><td></td></tr>"; fputs($fphtml,')</td><td></td></tr>');usleep(10000); break; case "CIF_NUMBER": //echo "</td></tr>"; fputs($fphtml,'</td></tr>');usleep(10000); break; case "PHONE_NUMBER": //echo "</td>"; fputs($fphtml,'</td>');usleep(10000); break; case "FAX_NUMBER": //echo "</td><td></td></tr>";
fputs($fphtml,'</td><td></td></tr>');usleep(10000); break; } } function character_data($parser,$data) { echo $data; } fputs($fphtml,'</body></html>');usleep(10000); fclose($fphtml); ?>

MY XML FILE

<?xml version="1.0"?>
<document>
<sender>
<sender_name>Albert Einstein</sender_name>
<address>any address</address>
<city>London</city>
<region>Europe</region>
<cp_sender>23345</cp_sender>
<cif_number>2356776</cif_number>
<phone_number>23456789</phone_number>
<fax_number>45678943</fax_number>
</sender>
</document>

    Well, first of all, you don't need all those usleep() commands in there. PHP doesn't need to wait for PHP.

    But your problem is that in the function start_element(), you never give $fphtml a value. Sure, you give it a value outside the function, but unless you say otherwise it will be treated as a different variable.

    function start_element(...)
    {
    global $fphtml;
    ...
    }
    

      Many thanks for answering. Now is working

        Write a Reply...