Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

php SAX parser in action

Sun Oct 26, 2008 6:46 pm

php code
<?
function start_element($parser, $element_name, $element_attrs) {
switch ($element_name) {
case 'KEYWORDS':
echo '<h1>Keywords</h1><ul>';
break;
case 'KEYWORD':
echo '<li>';
break;
}
}

function end_element($parser, $element_name) {
switch ($element_name) {
case 'KEYWORDS':
echo '</ul>';
break;
case 'KEYWORD':
echo '</li>';
break;
}
}


function character_data($parser, $data) {
echo htmlentities($data);
}

$parser = xml_parser_create();
xml_set_element_handler($parser, 'start_element', 'end_element');
xml_set_character_data_handler($parser, 'character_data');

$fp = fopen('keyword-data.xml', 'r')
or die ("Cannot open keyword-data.xml!");


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);
?>

keyword-data.xml
xml code
<!-- keyword-data.xml  -->
<keywords>
<keyword>XML</keyword>
<keyword>PHP</keyword>
<keyword>Perl</keyword>
<keyword>JavaScript</keyword>
<keyword>ASP</keyword>
</keywords>




Post a reply
  Related Posts  to : php SAX parser in action
 php simple XML parser     -  
 Command Line Parser Code Using C++     -  
 How to implement Command Line Parser in C++ Code     -  
 php extends in action     -  
 is_numeric in action     -  
 JButtons implementing an Action     -  
 Button action listener should implement abstract method     -  
 Two Listeners- Mutli- Action Listeners on Text Area     -  

Topic Tags

PHP XML