Page 1 of 1

php xml output problem, urgent plz help

Posted: Sat Jun 12, 2004 1:58 am
by gaurav_sting
hi everyone,

i am a newbie, trying to learn how to parse a xml document using php.
i got some code from some tutorial, the code has no errors but there is a problem

i am using php expert editor which has got inbulit web-server sought of a think that runs on port 8080. when i run the code from the php expert editor i am able to see the result (output).

on the other hand i am using apache 2.0. now when i run the script on using http://localhost/xml/parse.php - i do not get the desired output, i just get the text (echo "......."). and not the parsed output, i am including the code below, kindly help:

parse.php

Code: Select all

<?php

$compiled_langs = array();
$interprt_langs = array();
$flag = '';
$count = 0;

function opening_element($parser, $element, $attributes) {
    // opening XML element callback function

    global $flag;

    if ($element == 'languages')
        $flag = $attributes['type'];

}

function closing_element($parser, $element) {
    // closing XML element callback function

    global $flag;

    if ($element == 'languages')
        $flag = '';
}

function character_data($parser, $data) {
    // callback function for character data

    global $flag;

    if ($flag == 'compiled') {
        global $compiled_langs;
        $compiled_langs[] = $data;
    }

    if ($flag == 'interpreted') {
        global $interprt_langs;
        $interprt_langs[] = $data;
    }

}

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, 'opening_element', 'closing_element');
xml_set_character_data_handler($parser, 'character_data');

$document = file('sample.xml');

foreach ($document as $line) {
    xml_parse($parser, $line);
}

xml_parser_free($parser);

printf("The following compiled languages were found...\n");
foreach ($compiled_langs as $name) {
    $count++;
    printf("%' 3d. %s\n", $count, $name);
}

$count = 0;
printf("\nThe following interpreted languages were found...\n");
foreach ($interprt_langs as $name) {
    $count++;
    printf("%' 3d. %s\n", $count, $name);
}
?>
the xml file is:

Code: Select all

&lt;list&gt;
  &lt;languages type='interpreted'&gt;
    &lt;name&gt;PHP&lt;/name&gt;
    &lt;name&gt;Python&lt;/name&gt;
    &lt;name&gt;Ruby&lt;/name&gt;
  &lt;/languages&gt;
  &lt;languages type='compiled'&gt;
    &lt;name&gt;C&lt;/name&gt;
    &lt;name&gt;Fortan&lt;/name&gt;
    &lt;name&gt;Pascal&lt;/name&gt;
  &lt;/languages&gt;
&lt;/list&gt;

now then i run the code in php expert editor i get the following output:

The following compiled languages were found...
1. C
2. Fortran
3. Pascal

The following interpreted languages were found...
1. PHP
2. Ruby
3. Python


but when i run the script using http://localhost/xml/'parse.php, i get the following output (note apache is running on port 80)

The following compiled languages were found... The following interpreted languages were found


i dont know why is this happening, kidly help.

Gaurav Behl


feyd switched to [php.] and [code.] tags: viewtopic.php?t=21171

Posted: Sat Jun 12, 2004 4:03 am
by d3ad1ysp0rk
try running this file on your computer (localhost):

Code: Select all

<?php echo "test."; ?>
and tell us what you get for output (either test, or echo "test";).