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);
}
?>Code: Select all
<list>
<languages type='interpreted'>
<name>PHP</name>
<name>Python</name>
<name>Ruby</name>
</languages>
<languages type='compiled'>
<name>C</name>
<name>Fortan</name>
<name>Pascal</name>
</languages>
</list>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